Skip to content

Allow index [key: string] to be typed - only allow certain strings for the index #32489

Closed
@ORESoftware

Description

@ORESoftware

Search Terms

keyof arrays typeof index

Suggestion

const Events = <const> [
  'repo:push',
  'pullrequest:unapproved',
  'pullrequest:created'
];

type MyEvents = typeof Events[number];

export interface InterosTag {
  [key: string]: {
    [key: keyof MyEvents]: {   // this part does not work
      "jenkins-job": string,
      "deploy": boolean,
      "eks-key": string
    }
  }
}

Examples

I am trying to create a type for this:

const needsTypeInfo = {
      "events": {
              "repo:push": {
                "jenkins-job": "build_entity_tools",
                "deploy": false,
                "eks-key": "entity-tools"
            },
              "pullrequest:unapproved": {
                "jenkins-job": "build_entity_tools",
                "deploy": false,
                "eks-key": "entity-tools"
            },
            "pullrequest:unapprovedXXX": {
                "jenkins-job": "build_entity_tools",
                "deploy": false,
                "eks-key": "entity-tools"
            }
      }     
}

the key with XXX should fail because it's not in the list:

const Events = <const> [
  'repo:push',
  'pullrequest:unapproved',
  'pullrequest:created'
];

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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