String('str')
is not an instance of type String
:
// false
String('str') instanceof String
// true
typeof String('str') === 'string'
new String('str')
will create a wrapper object around 'str'
which leads to:
// true
new String('str) instanceof String
// false
typeof new String('str') === 'string'
For me, this means
const myString: String = String('str')
Should generate a compile error because of invalid type, but it does not. Whereas:
const myString: String = new String('str')
Does not generate a compile error as well. This applies to the other primitive wrapper objects as well.
Is this by design? If so, why? If I want to differentiate between new String()
and String()
, I can't, which should be able just for type safety at compile time, but also at runtime with reflect-metadata
, or am I missing something?