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
Type inference for constant objects with string literal types #32139
For the first object the compiler can infer type of hello as a string literal type and outputs no errors. However, for the second object can't infer this, although obj is a constant object, and throws an error.
It would be nice to have automatic inference here. For example, in Flow types are inferred here automatically and the same code passes type checks without errors.
Addition: specifying the string type literal explicitly helps:
constobj={hello: 'world'as'world'}
but makes the code look verbose.
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
However, for the second object can't infer this, although obj is a constant object, and throws an error.
The variable obj is constant, but the object is not. You can re-assign a new value with obj.hello = "fubar"; just fine.
What you want can be achieve with the "const contexts" feature introduced in TypeScript 3.4: #29510
const obj = { hello: 'world' } as const;
[x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
This would definitely be a breaking change in existing code. If it would infer obj.hello as the type 'world', then re-assigning a different string would not work anymore.
I was actually thinking about was more similar to automatic inference of "const context" in some cases.
I see. Personally I think it's really not worth the additional code (complexity) and performance cost (the analysis), when we already have a very good feature to solve this without hassle: const contexts.
Activity
MartinJohns commentedon Jun 27, 2019
The variable
objis constant, but the object is not. You can re-assign a new value withobj.hello = "fubar";just fine.What you want can be achieve with the "const contexts" feature introduced in TypeScript 3.4: #29510
This would definitely be a breaking change in existing code. If it would infer
obj.helloas the type'world', then re-assigning a different string would not work anymore.MartinJohns commentedon Jun 27, 2019
I see. Personally I think it's really not worth the additional code (complexity) and performance cost (the analysis), when we already have a very good feature to solve this without hassle: const contexts.
typescript-bot commentedon Jun 29, 2019
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.