Skip to content

Define literal type during object declaration #14194

Closed
@kube

Description

@kube

Problem

During object declaration, there is no way to explicitly define a property literal type:

const obj = {
  hello: 'world'
}

Here typeof obj is { hello: string }

Example of Use Case

In the case of a Redux Action, which will be constant, I expect type property to be a literal type:

const increment = {
  type: 'INCREMENT'
}

const decrement = {
  type: 'DECREMENT'
}

export type Action =
  & typeof increment
  & typeof decrement

Current Workaround

It's possible to explicitly define the literal type after the value:

const increment = {
  type: 'INCREMENT' as 'INCREMENT'
}

Which causes two problems:

  • redundancy
  • misspelling won't be analysed ('foo' as 'bar' will work)

readonly would be a solution, but does not seem to be compatible with object declaration syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions