Search code examples
reactjstypescripttypescript-typingsreact-tsxtypescript-types

One of two strings as state in React with Typescript


I have a component that looks like this:

export interface Props {
  // ... some props
}

export interface State {
  readonly mode: "add" | "delete"
}

export class MyComponent extends Component<Props, State> {
  readonly state = { mode: "add" }
  // ... more component code
}

The problem is this throws the linting error:

Property 'state' in type 'ScannerScreen' is not assignable to the same property in base type 'Component<Props, State, any>'.
  Type '{ mode: string; }' is not assignable to type 'Readonly<State>'.
    Types of property 'mode' are incompatible.
      Type 'string' is not assignable to type '"add" | "delete"'.

Why does TypeScript not recognize that "add" or "delete" are strings or that "add" is one of the allowed types for mode?


Solution

  • This is due to type inference -- TypeScript will infer 'add' as string rather than the type 'add'. You can fix this pretty easily by doing: mode: "add" as "add". You can also use a type annotate for the state: readonly state: State = { mode: "add" }.