You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Infer type when object coerced to string if toString explicitly defined #35945
Infer type when object coerced to string if toString explicitly defined
Examples
Would be nice if the following would be allowed
constaction={type: 'foo',toString(){returnthis.type}}asconst;constreducerHandler={[action]: ()=>{}// Currently errors: A computed property name must be of type 'string', 'number', 'symbol', or 'any'.}
changed the title [-]Infer type when object coerced to string if `toString` explicitly defined[/-][+]Infer type when object coerced to string if toString explicitly defined[/+]on Jan 1, 2020
As it stands today, TS doesn't really allow for explicit toString, which in some sense makes it incompatible with regular JS. Unless you give up pretty much all type safety for a given value. Adding { toString(): string } to your types essentially means you accept everything. I'm not sure if automatically accepting toString where string literal is expected would be a good idea, however a way to specific that explicit toString is accepted, would help with many different cases use-cases.
for this to work safely, would require to instruct the compiler with a type like string | { toString(): string }, or somehow a new conventional type like tostring ? serializable, stringprovider, etc...
Activity
[-]Infer type when object coerced to string if `toString` explicitly defined[/-][+]Infer type when object coerced to string if toString explicitly defined[/+]rihok commentedon Aug 27, 2020
I think this would definitely be useful.
As it stands today, TS doesn't really allow for explicit toString, which in some sense makes it incompatible with regular JS. Unless you give up pretty much all type safety for a given value. Adding
{ toString(): string }to your types essentially means you accept everything. I'm not sure if automatically accepting toString where string literal is expected would be a good idea, however a way to specific that explicit toString is accepted, would help with many different cases use-cases.isometriq commentedon Jun 15, 2021
for this to work safely, would require to instruct the compiler with a type like
string | { toString(): string }, or somehow a new conventional type liketostring? serializable, stringprovider, etc...