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
Currently, it is not possible (or I missing some crucial knowledge here) to infer the concrete type of the value of an object field. So while in the following code the compiler correctly infers bar for a
consta='bar'typeb=(typeofa);// this yields "foo"
it fails to do the equivalent in the following example
constt={foo: 'bar'};typeu=(typeoft)['foo'];// this yields string
The compiler should literal types for values in objects, as well.
Use Cases
I would like to be able to infer the types of configuration objects without having to explicitly annotate them. This would allow me to write such objects without much hassle and simultaneously use Typescripts inference mechanism to reason about the well-definedness of these values.
Checklist
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.)
@jack-williams Yes, I guess this is a specific case of the more general inference problem mentioned in #10195. I summarized my findings there, as well.
Search Terms
index signature, object, value, field
Suggestion
Currently, it is not possible (or I missing some crucial knowledge here) to infer the concrete type of the value of an object field. So while in the following code the compiler correctly infers
bar
fora
it fails to do the equivalent in the following example
The compiler should literal types for values in objects, as well.
Use Cases
I would like to be able to infer the types of configuration objects without having to explicitly annotate them. This would allow me to write such objects without much hassle and simultaneously use Typescripts inference mechanism to reason about the well-definedness of these values.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: