Skip to content

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

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

Closed
5 tasks done
ORESoftware opened this issue Jul 19, 2019 · 2 comments
Closed
5 tasks done
Labels
Duplicate An existing issue was already created

Comments

@ORESoftware
Copy link

ORESoftware commented Jul 19, 2019

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.
@fatcerberus
Copy link

#26797

@ORESoftware
Copy link
Author

ORESoftware commented Jul 20, 2019

Yeah in particular, I was hoping that this would work as is:

const Events = [
  'pullrequest:rejected',
  'pullrequest:fulfilled',
  'pullrequest:created'
];

export interface InterosTag {
    [key: keyof Events]: {    // use keyof here (or some related operator)
      "jenkins-job": string,
      "deploy": boolean,
      "eks-key": string
   }
}

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 31, 2019
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