1

How do you convert template literals into numbers in typescript when defining a type?

I'm ultimately trying to add the negative values of the keys of an object to the type. I'd prefer not to type out the negative values explicitly.

Contrived example

const obj = {1:1, 2:2, 3:3, auto:4};

type MyType = keyof typeof obj | `-${Exclude<keyof typeof obj | "auto">}` 
// can you somehow parseFloat() that string literal

// Should accept 1, 2, 3, -1, -2, -3, "auto"
// Currently it accepts the negative values as strings only
wongz
  • 3,255
  • 2
  • 28
  • 55
  • No, there's no way to do this automatically. See the answers to the linked questions. You can't even abuse tuple to give you *negative* number literals, so the answer here seems to be just "no", unfortunately. – jcalz Jan 05 '22 at 19:42

0 Answers0