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

'yes' | 'no' converted to string when in react state #37005

Closed
matthewoates opened this issue Feb 25, 2020 · 2 comments
Closed

'yes' | 'no' converted to string when in react state #37005

matthewoates opened this issue Feb 25, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@matthewoates
Copy link

matthewoates commented Feb 25, 2020

Search Terms:
enums react, unions broken, react union broken, react state union

Code

import React, { Component } from 'react';

type PropsType = {};
type StateType = {
    ans: 'yes' | 'no'
}

// this works
const data: StateType = { ans: 'yes' };

export default class ExampleComponent extends Component<PropsType, StateType> {
    // error
    state = { ans: 'yes' };

    constructor(props: PropsType) {
        super(props);

        // this works
        this.state = { ans: 'yes' };
    }
}

Playground Link

Expected behavior:
no errors

Actual behavior:
error on state = { ans: 'yes' };

(property) ExampleComponent.state: {
    ans: string;
}
Property 'state' in type 'ExampleComponent' is not assignable to the same property in base type 'Component<PropsType, StateType, any>'.
  Type '{ ans: string; }' is not assignable to type 'Readonly<StateType>'.
    Types of property 'ans' are incompatible.
      Type 'string' is not assignable to type '"yes" | "no"'.(2416)
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 25, 2020
@RyanCavanaugh
Copy link
Member

Duplicate #10570. You can use { ans: 'yes' } as const" in the meantime.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants