0

I want to create a type for a year state in React which should be a number with 4 digits :

type Year = number & **what should I add here to limit to 4 digits**

  • Potentially related question covering strings: [Declaring string type with min/max length in typescript](https://stackoverflow.com/questions/51813272/declaring-string-type-with-min-max-length-in-typescript) – DBS Aug 22 '23 at 16:29
  • Use branded/opaque types `1999 as number & {_length: 2}` , no point to make it an actual type – Dimava Aug 22 '23 at 16:33
  • What's the use case? You can define `Year` as a large union (4 digits is small enough to fit in a TS union) like [this](https://tsplay.dev/NdOv0w), but who knows if that's actually what you want? Are you validating compile-time numeric literals? Or just runtime values? – jcalz Aug 22 '23 at 16:38

0 Answers0