-
Notifications
You must be signed in to change notification settings - Fork 12.8k
JSDoc: params for typed function are inferred as any #16220
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
Comments
looks like a duplicate of #15618 (comment), the contextual type from the |
This also appears to be effecting classes: import React, { Component, ComponentClass } from 'react';
/** @typedef { {
foo: string,
} } Props */
/** @type {ComponentClass<Props>} */
const MyComponent = class extends Component {
componentDidMount() {
this.props.foo; // <-- foo is inferred as any, expected string
}
};
<MyComponent foo={1} /> // <-- error as expected: 1 is not assigned to string @mhegazy Is this the same bug as above, or does it need filing separately? |
use |
Thanks @mhegazy. How come inference works with that syntax but not with To be clear, the example I gave uses a class expression, not a class declaration, so I can't see why const MyComponent: ComponentClass<Props> = class extends Component<any, any> {} |
it is not really a type that is missing, it is that |
Thanks! That fixes it. For context, the reason I am using the import React, { Component, ComponentClass } from 'react';
/** @typedef { {
foo: string,
} } Props */
/** @type {ComponentClass<Props>} */
/** @augments {Component<Props, any>} */
const MyComponent = class extends Component {
componentDidMount() {
this.props.foo; // <-- foo is inferred as string
}
};
MyComponent.displayName = 1; // <-- error as expected: 1 is not assignable to string
<MyComponent foo={1} /> // <-- error as expected: 1 is not assignable to string This is equivalent to the following TypeScript: import React, { Component, ComponentClass } from 'react';
type Props = {
foo: string,
};
const MyComponent: ComponentClass<Props> = class extends Component<Props, any> {
componentDidMount() {
this.props.foo; // <-- foo is inferred as string
}
};
MyComponent.displayName = 1; // <-- error as expected: 1 is not assignable to string
<MyComponent foo={1} /> // <-- error as expected: 1 is not assignable to string Given that this effects anyone who wants to use TypeScript with React, I'm wondering if there's any way to consolidate these two types into one, somehow: DefinitelyTyped/DefinitelyTyped#16967 I also mentioned this in this issue: #14600 (comment) |
Just tested on typescript@2.4.1-insiders.20170614 and this is fixed. Thanks! |
TypeScript Version: 2.3.4
Code
The text was updated successfully, but these errors were encountered: