Skip to content
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

Maintain string litteral types when used as index signature type #17784

Closed
mohsen1 opened this issue Aug 14, 2017 · 2 comments
Closed

Maintain string litteral types when used as index signature type #17784

mohsen1 opened this issue Aug 14, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@mohsen1
Copy link
Contributor

mohsen1 commented Aug 14, 2017

TypeScript Version: 2.4.0

Code

function makeObj(foo: 'FOO') {
    return { [foo]: 1 };
}

makeObj('FOO') // return type: `{ [x: string]: number}`

Expected behavior:
Index signatures should have narrower types. Return type should be { [x: 'FOO']: number}

Actual behavior:
return type is: { [x: string]: number}

Use case

When making forms in React components I often use a method that returns an onChange method to be used on input onChange properties:

class Component<S> {
    state: S;
    setState(partialState: Partial<S>) { }
    onFiledChanged = (filedName: keyof S) => (value) => {
        this.setState({ [filedName]: value });
    }
    render() {
        return <form>
                <input onChange={ this.onFiledChanged('foo') } />
                <input onChange={ this.onFiledChanged('bar') } />
            </form>
    }
}

TypeScript fails to understand filedName is type 'foo' | 'bar'

@mhegazy
Copy link
Contributor

mhegazy commented Aug 22, 2017

looks like a duplicate of #16687

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 22, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 6, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Sep 6, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants