We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
Fixes #6041.
With this PR an object literal type is assignable to a type with an index signature if all known properties in the object literal are assignable to that index signature. This makes it possible to pass a variable that was initialized with an object literal as parameter to a function that expects a map or dictionary:
function httpService(path: string, headers: { [x: string]: string }) { } const headers = { "Content-Type": "application/x-www-form-urlencoded" }; httpService("", { "Content-Type": "application/x-www-form-urlencoded" }); / Ok httpService("", headers); / Now ok, previously wasn't
The PR adds the following three assignment compatibility rules (where an object literal type is the inferred type of an object literal or a type declared using an object type literal with no call or construct signatures):
The PR adds corresponding type inference rules.
The PR furthermore removes the rule that adds index signatures to an object literal when it is contextually typed by a type that has index signatures. Instead, the type inferred for an object literal has index signatures only if it contains computed properties. For example:
let s = "hello"; let n = 123; let o = { [s]: new Date(), [n]: new Error() };
In the above, the type of o is { [x: string]: Date | Error, [x: number]: Error }.
o
{ [x: string]: Date | Error, [x: number]: Error }
Removing the automatic contextual index signatures from object literal types has the nice effect of reducing noise in our error messages. Also, we can now report the name of the offending property when an object literal doesn't meet the constraint implied by a target index signature (previously you'd have to deduce it yourself from two incompatible union types).
Sorry, something went wrong.
Implicit index signatures for object literal types
1d428b9
Accepting new baselines
837e6db
Include index signatures in object literal types only when computed p…
040effd
…roperties are present
d9fc61b
Adding test
bb7ba1d
Simplify indexTypesRelatedTo function
5f95c75
Infer index signatures when object literals contain computed properties
b724a09
678d591
Removing unused function
1af4e1c
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
... " with no call/construct signatures"
👍
Fixing comment
a1040f0
Can you not report the index signature here? This is probably confusing for users, especially if the index signature type ever gets cut out.
I dunno. I think it is pretty decent the way it is now and the last line makes it completely clear what isn't compatible with what.
It's not clear what the type of p is without the full type, so it'd be better to just report the index signature given that it'd take relatively little effort on our part.
p
Agreed, I don't see how it would be better if we didn't show the index signature
Right, but the whole point of our elaboration scheme is to not try to show everything on one line. The respective types of p and the index signature follow in the next elaboration. It's completely analogous to how we report a mismatch between two properties.
Merge pull request #7029 from Microsoft/implicitIndexSignatures
a8633ee
Implicit index signatures
Sad that I'm not going to rake in 30-100 Stack Overflow rep every week for my answer on this one anymore.
@RyanCavanaugh Heh.
I just tried this out. Shouldn't a type parameter inherit the index signature?
I have the following class:
export abstract class Component<P, T extends { [index: string]: string }, E extends { [element: string]: DOMElement }> extends EventEmitter {
And I was hoping I could just write:
export abstract class MyComponent<P, T, E> extends Component<P, T, E> {
And it would just inherit the index signature. But instead I need to explicitly write the constrained index signature. Which is quite painful, since I have many subclasses of Component.
Component
Ok seems like this PR doesn't work for type arguments? Just passing a type, as a type argument, with all members satisfying the index signature doesn't work for me.
I would also love to have what @tinganho wants.
My use case is the following:
interface ITest { [x: string]: string } class Test<T extends ITest> { content: T } const myObject = { test: "hello" }; class MyTest extends Test<typeof myObject> { / Error, 'typeof myObject' doesn't implement the index signature... method() { console.log(this.content.test) } }
I'm planning to do this extensively with model-generated object litterals (like myObject here) and it'd be nice not to have to also generate the corresponding interface for all those litterals... it could work out though, so I guess it's not that big of an issue.
myObject
If you want to keep forcing the index signature to be respecified in a derived interface, maybe then my issue could be fixed by typeof infering an index signature if applicable, if that's not too stupid of an idea.
typeof
Update AngularJS to 1.5
65347ef
Asked this in #14736
Why this case:
function httpService(path: string, headers: { [x: string]: string }) { } interface MyHeaders { 'Content-Type': string } const headers: MyHeaders = { "Content-Type": "application/x-www-form-urlencoded" }; httpService("", { "Content-Type": "application/x-www-form-urlencoded" }); / Ok httpService("", headers); / NOT OK
is NOT valid, the error is:
Argument of type 'MyHeaders' is not assignable to parameter of type '{ [x: string]: string; }'. Index signature is missing in type 'MyHeaders'.
Successfully merging this pull request may close these issues.