1

If I wrote [key: string]: string they both work. I am curious why interface is not working when I use in keyword here?

enter image description here

type Keys = "username" | "password" | "confirm" | "captcha";

export interface InputData {
    [key in Keys]: string; //no good
}

export type InputData2 = {
    [key in Keys]: string; //good
}

Here is the error:

enter image description here

A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1169)
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)
'Keys' only refers to a type, but is being used as a value here.ts(2693)
any
Tom
  • 15,781
  • 14
  • 69
  • 111
  • 1
    [Please replace/supplement images of code/errors with plain text versions](https://meta.stackoverflow.com/a/285557/2887218). – jcalz Dec 01 '21 at 16:40
  • 1
    Index signatures and mapped types are different despite having different syntax; see [this question/answer](https://stackoverflow.com/a/59331699/2887218). I think this question should probably be closed as a duplicate of that one unless you can [edit] the question to significantly distinguish them. – jcalz Dec 01 '21 at 16:42
  • 1
    Ah there's also https://stackoverflow.com/questions/69635210/typescript-cannot-declare-additional-properties-on-mapped-types – jcalz Dec 01 '21 at 16:48
  • Thank you jcalz. I am reading your answer to the other question trying to wrap my head around it. Maybe your answer hits two questions at once. However, on the surface I don't see how they are duplicated. – Tom Dec 01 '21 at 16:49
  • 1
    I still see only an image of errors in there; plain text is better for the reasons outlined in the link in my first comment. – jcalz Dec 01 '21 at 17:02

0 Answers0