2

Why does TypeScript let me assign a string primitive to a variable of type String (the object type)? E.g., why doesn't this cause an error:

var str : String = '2333';

Note that the other way doesn't work:

var str : string = new String('2333'); // Error: Type 'String' is not assignable to type 'string'. 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.

Codes as below:

const str: string = '2333'
console.log(`typeof str === "string" is ${typeof str === "string"}`)

const str2: String = new String('2333')
console.log(`str2 instanceof String is ${str2 instanceof String}`)
console.log(`typeof str2 === "string" is ${typeof str2 === "string"}`)

const str3: String = '2333'
console.log(`str3 instanceof String is ${str3 instanceof String}`)
console.log(`typeof str3 === "string" is ${typeof str3 === "string"}`)

it puts:

typeof str === "string" is true
str2 instanceof String is true
typeof str2 === "string" is false
str3 instanceof String is false
typeof str3 === "string" is true

from str and str2 we can know the string is a diffrent type from String. Why str3 allow the misuse of two types?

0

1 Answer 1

1

The short version: TypeScript specifically allows string to be assigned to String.

The long version: TypeScript allows a string, number, or boolean primitive to be assigned to a variable of its associated object type. This is defined in sections §3.11.4 and §3.11.1 of the spec.

§3.11.4 says:

S is assignable to a type T, and T is assignable from S, if S has no excess properties with respect to T (3.11.5) and one of the following is true:

  • S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, one of the following is true:
    • M is a property and S has an apparent property N where
      • M and N have the same name,
      • the type of N is assignable to that of M,
      • if M is a required property, N is also a required property, and
      • M and N are both public, M and N are both private and originate in the same declaration, M and N are both protected and originate in the same declaration, or M is protected and N is declared in a class derived from the class in which M is declared.
    • M is an optional property and S has no apparent property of the same name as M.
    • M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, when M and N are instantiated using type Any as the type argument for all type parameters declared by M and N (if any),
      • the signatures are of the same kind (call or construct),
      • M has a rest parameter or the number of non-optional parameters in N is less than or equal to the total number of parameters in M,
      • for parameter positions that are present in both signatures, each parameter type in N is assignable to or from the corresponding parameter type in M, and
      • the result type of M is Void, or the result type of N is assignable to that of M.
    • M is a string index signature of type U, and U is the Any type or S has an apparent string index signature of a type that is assignable to U.
    • M is a numeric index signature of type U, and U is the Any type or S has an apparent string or numeric index signature of a type that is assignable to U.

Then §3.11.1 says:

The apparent members of the primitive type String and all string literal types are the apparent members of the global interface type 'String'.

Pulling out the parts of those that are relevant:

S (string in our case) is assignable to a type T (String in our case)...if S has no excess properties with respect to T, ... S is...the Number, Boolean, or String primitive type, T is an object type...and for each member M in T...M is a property and S has an apparent property N where...M and N have the same name; the type of N is assignable to that of M; if M is a required property, N is also a required property; and M and N are both public...

...because string's apparent members are String's apparent members.

Other parts of §3.11 and various other sections (such as §3.2.3) touch on the relationship between a primitive type and its associated object type as well.

4
  • Hello T. J. Crowder, you're not answering to all the question, either. The question is about str3. Look at my awesome answer. ;-)
    – Paleo
    Sep 21, 2017 at 10:17
  • @Paleo: The str3 case is exactly what I answered above. Sep 21, 2017 at 10:31
  • @Paleo @T.J. Crowder : I think if TypeScript allow the use of str3, it would be better if typescript compile str3 instanceof String to (str3 instanceof String || typeof str3 === "string")
    – xiang
    Sep 23, 2017 at 8:56
  • @xiang: You're confusing assignment compatibility with type identity. :-) TypeScript doesn't say string and String are the same thing, it says that you're allowed to assign string to a variable of type String. That doesn't make the actual string a String. (All of that said, the whole concept of having both string and String is highly suspect, but we're stuck with it... :-) ) Sep 23, 2017 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.