Closed Shinigami92 closed 2 years ago
You could do something like this
interface ID<A>extends Newtype<{ readonly ID: unique symbol; readonly a: A }, string> {}
interface CatID extends ID<'Cat'> {}
interface DogID extends ID<'Dog'> {}
declare const c: CatID
export const d: DogID = c // Type 'CatID' is not assignable to type 'DogID'.
I don't suggest you do to so though, because you can provide a unique symbol
to ID
, so nothing is preventing to do this
interface HorseID extends ID<'Cat'> {}
You could do something like this
interface ID<A>extends Newtype<{ readonly ID: unique symbol; readonly a: A }, string> {} interface CatID extends ID<'Cat'> {} interface DogID extends ID<'Dog'> {} declare const c: CatID export const d: DogID = c // Type 'CatID' is not assignable to type 'DogID'.
I don't suggest you do to so though, because you can provide a
unique symbol
toID
, so nothing is preventing to do thisinterface HorseID extends ID<'Cat'> {}
Thanks, it's working I will ignore your warning, cause these kind of extending newtypes and make it more compile time safe is better then wrong written code by human. It would be the same reason for me to use or not to use TypeScript at all :wink:
📖 Documentation
I have in example the following setup:
Sadly I don't get any error for
const lid2: PersonId = cid;
How do I correctly create a sub type of
ID
?