Closed fathyb closed 7 years ago
This is intended behavior as we widen 'literal'
to string
in an object literal with no contextual type.
If you instead were to write:
const obj: { type: 'literal' } = { type: 'literal' };
const type2 = obj.type;
Then you would see the behavior you anticipate. We do not infer 'literal'
for obj.type
in this case is it would break many other use cases of const obj = { x: 'a' }; obj.x = 'b';
.
TypeScript Version: 2.3.4
Code
Expected behavior: It should accept string literals from objects
Actual behavior: We need to manually cast the variable to the literal type