Skip to content

Define literal type during object declaration #14194

Closed

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.

Activity

  1. mhegazy commented on Feb 8, 2018

    @mhegazy
    Contributor

    Duplicate of #17363 and #20271 and others. please see #20271 (comment) and #10676 for more context.

  2. added
    DuplicateAn existing issue was already created
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Feb 8, 2018
  3. typescript-bot commented on Feb 26, 2018

    @typescript-bot
    Contributor

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

  4. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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