This project is read-only.

interfaces that abbreviate primitive types

Topics: General, Language Specification
Jul 1, 2014 at 4:46 PM
So, it's cool that an interface can be used to abbreviate a function (

I think it would be cool to be able to able to extend strings/primitive types too. If it worked, it would let you do nice things like I confusingly tried here:

I was trying to use an interface to describe a string that is assumed to have a certain structure; it's like a comment for the intended use-case of the string.

Ideally you'd be able to create proper abstract type that requires syntax to convert (and otherwise it protects you from using one kind of string in place of another string.

e.g. one might imagine:
interface StringThing {

function isFoo(s:StringThing) : boolean {
  return (s === 'foo');
Although perhaps it would be better to just support either type defs ( or abstract types/interfaces (

Along the way, I was wondering why an interface cannot extend primitive type, because that could be cool too.
interface StringThing extends string {}
Jul 2, 2014 at 5:16 AM
I think it would be cool to be able to able to extend strings/primitive types too.
It's actually not possible to extend primitive types, and have them behave exactly the same way. Primitive types create special native host objects. For example, the the array object updates the "length" property as items are set directly by index (like myarray[10]=10). Extending primitive arrays would mean losing that ability. There is no way to replicate that in all older browsers (note: the various functions could still work however). Also, the native string constructor creates a character array of the string contents, and that feature would be lost as well. Why? Because there is no way to apply constructors of primitive types to new user objects.

With new ES6 features, however, this may be possible to some degree (if not entirely), but it would not be backward compatible.
Jul 2, 2014 at 5:37 AM
Sorry, I just realized you were talking about using interfaces to extend primitive types (reading too fast). I think this open issue would address this already:
Jul 2, 2014 at 2:56 PM
Yeah, that looks like it would work. Cool!