-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resulting type of the object literal #16130
Comments
This is a place for reporting and discussing issues, not a help forum. |
@Draqir first of all I have asked the question before at stackoverflow and haven't got an answer. |
First of all, the spec is definitely not up to date (it currently describes 1.8).
declare function f<T extends { [x: string]: boolean | string | number }>(x: T): T;
export let x = f({
a: 100,
b: 200,
c: 'hello'
}) Hover over the type of
This is mostly a symptom of the spec being out of date. With indexed access types, we changed a lot of the behavior here. Check out #11929 for specifics. |
@DanielRosenwasser thanks for example. If you are taking about export declare let x: {
a: 100;
b: 200;
c: "hello";
}; If you are talking about Let me make parsing of your example applying next section of spec to understand terms in the same way: When an object literal is contextually typed by a type that includes a string index signature, the resulting type of the object literal includes a string index signature with the union type of the types of the properties declared in the object literal, or the Undefined type if the object literal is empty Object literal is |
@DanielRosenwasser https://github.com/Microsoft/TypeScript/blob/master/src/compiler/checker.ts#L13412 const result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); |
I asked question about resulting type of the object literal at stackoverflow.
From specification:
When an object literal is contextually typed by a type that includes a string index signature, the resulting type of the object literal includes a string index signature with the union type of the types of the properties declared in the object literal, or the Undefined type if the object literal is empty
First, it sounds like some entity that has type can redefine it if it meets object literal.
Second, maybe it is all about hidden conversations at expression trees?
The question:
Is there any places in code where I can see resulting type of object literal in action?
I have got an answer:
Ok.
I look at following part of spec.
A bracket notation property access of the form
object [ index ]
where object and index are expressions, is used to access the property with the name computed by the index expression on the given object. A bracket notation property access is processed as follows at compile-time:
If index is a string literal or a numeric literal and object has an apparent property (section 3.11.1) with the name given by that literal (converted to its string representation in the case of a numeric literal), the property access is of the type of that property.
Otherwise, if object has an apparent numeric index signature and index is of type Any, the Number primitive type, or an enum type, the property access is of the type of that index signature.
Otherwise, if object has an apparent string index signature and index is of type Any, the String or Number primitive type, or an enum type, the property access is of the type of that index signature.
Otherwise, if index is of type Any, the String or Number primitive type, or an enum type, the property access is of type Any.
Otherwise, the property access is invalid and a compile-time error occurs.
The type of
str
is union of string literals and there is no branch for this case above.And I didn't see any index signatures in the example (from answer).
But it does compile.
Could someone elaborate on this topic, please.
The text was updated successfully, but these errors were encountered: