-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type checker doesn't understand string literal type in computed property key #15534
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Duplicate of #5579 |
Here's a more real-world example where this would be helpful, in the context of React: import * as React from 'react';
import { ChangeEvent } from 'react';
interface LoginState { username: string; password: string; }
export class Login extends React.Component<{}, LoginState> {
public state: LoginState = { username: '', password: '' };
private onChange(event: ChangeEvent<HTMLInputElement>, property: keyof LoginState) {
this.setState({ [property]: event.target.value }); // this doesn't work!
}
public render() {
return (
<form>
<input value={this.state.username}
onChange={(e) => this.onChange(e, 'username')}/>
<input type="password"
value={this.state.password}
onChange={(e) => this.onChange(e, 'password')}/>
<input type="submit" value="Login"/>
</form>
);
}
} Relevant definition of class Component<P, S> {
setState<K extends keyof S>(state: Pick<S, K>, callback?: () => any): void;
} Right now the commented line in We currently bypass this by calling |
I have a similar issue.
|
This bug should be fixed. I am also having this issue. See this post on Stackoverflow: https://stackoverflow.com/questions/46361905/property-is-missing-in-type-x-string-string |
This should be fixed in by #18317, give |
TypeScript Version: nightly (2.4.0-dev.20170502)
Code
Expected behavior:
Both calls typecheck.
Actual behavior:
The text was updated successfully, but these errors were encountered: