How to use interpolation in declaring a property of a type?

Asked

Viewed 52 times

4

I’m trying to declare a guy who accepts different properties with a number, for example: prop1, prop2 ... propN. The point is, I’m having a hard time getting it to behave the way I expected it to, and I don’t understand why.

The code below has been tested in version 4.3.5 of Typescript:

type Props = {
  [K in `prop${number}`]: boolean;
};

const myString = 'a';
const myNumber = 32;

const obj: Props = {
  prop15: true,
  propA: false,
  [`prop${myNumber}`]: true,
  [`prop${myString}`]: false,
  anything: 137,
}

const obj2: Props = {};

const someNumber: Props = 32;

See on Playground.

But it seems that type is not serving any purpose. None of the above code statements gave error. Even, when I put the cursor over Props, appears type Props = {}.

  • What’s wrong with this guy, who’s taking anything?
  • Is there any way to do what I need? Create a type that allows prop1, prop2 ... propN and nothing more?
  • But what the use-case for that? It doesn’t make much sense to type an object where several of the infinite possible properties wouldn’t exist. In that case, there’s no point in using a unknown?

  • 1

    Anyway, the fact that no error is reported when trying to use an infinite set of key generator for an object seems to me a compiler bug. I know it’s not the same, but look at the error that’s being issued in this case.

  • 1

    use-case: I’m using express-Validator validation with wildcards, something like 'addresses.*.postalCode' that would return me an object of error addresses[posição].postalCode. It doesn’t sound like a good name for property, but for now I’ve been leaving it that way and I was trying to create a type for it, when I came across the problem described in the question. It seems to me that Typescript 4.4 has something that maybe I could fix it, but I couldn’t use it properly.

1 answer

-1

Tries

type Props = {
  [K: `props${number}`]: boolean;
};

Also, this type of types Template Literal Types only work with the version of typescript 4.4 and above.

UPDATING
This answer does not catch the error [`props${myString}`]: false,

  • Actually this is from Typescript 4.4, no? Anyway, I tested and still does not work fully as expected, see on Playground. In the question example, it does not point out an error to [\prop${myString}`]: false`, you can tell why?

  • In reality it seems that any string concatenated, included ['sdsdf'+'sdfsd']: false, does not generate an error. Interesting. I’ll give you a few more searches.

  • Can I ask your question in the English OS? I put a link to this one. @Rafaeltavares

  • Of course, but it does not make sense to put the link, since here it is in English. And it is more appropriate to write the doubt in your words, because your level of understanding of the problem may be different from mine. I don’t know if Typescript accepts questions in the Github repository, it’s worth a look as well.

  • here is the link to the question I asked. I’ll update my answer if anyone finds out. https://stackoverflow.com/questions/68954369/template-literal-types-no-working-with-string-concatenation. @RafaelTavares

Browser other questions tagged

You are not signed in. Login or sign up in order to post.