Skip to main content
added 98 characters in body
Source Link
Filly
  • 439
  • 3
  • 9

So you can do something like this:


// all application keys will be defined here
// as const is needed here so typescript won't start widening the type to string[]
const applications = ["ELECTRIC", "HYDRAULIC", "PNEUMATIC"] as const

// get all keys as a Union Type
type ApplicationKeys = typeof applications[number]
// create your enum object as a type
type Application = { [Key in ApplicationKeys]: ApplicationProps<Key> }
// define your props with a key so typescript can differentiate between your enums
type ApplicationProps<T> = {
  key: T,
  propA: boolean;
  propB: number;
  propC: string;
}
// the actual enum definition
const application: Application = {
  ELECTRIC: { key: "ELECTRIC", propA: true, propB: 11, propC: "eee" },
  HYDRAULIC: { key: "HYDRAULIC", propA: false, propB: 59, propC: "hhh" },
  PNEUMATIC: { key: "PNEUMATIC", propA: true, propB: 87, propC: "ppp" },
}
// test
let foo = application.ELECTRIC;
let bar: Application["PNEUMATIC"]; 
bar = application.HYDRAULIC; // error: Type 'ApplicationProps<"HYDRAULIC">' is not assignable to type 'ApplicationProps<"ELECTRIC">'. 
bar ===foo // error his condition will always return 'false' since the types have no overlap

The neat part is that if you want to add a new application key you just have to add it to the list and typescript will throw errors for the missing or unhandled key. if you use a proper never assertion in a switch or if statementsstatement(exhaustive switch block)[https://stackoverflow.com/questions/39419170/how-do-i-check-that-a-switch-block-is-exhaustive-in-typescript].

I hope you understand my bad english :) CODE

So you can do something like this:


// all application keys will be defined here
// as const is needed here so typescript won't start widening the type to string[]
const applications = ["ELECTRIC", "HYDRAULIC", "PNEUMATIC"] as const

// get all keys as a Union Type
type ApplicationKeys = typeof applications[number]
// create your enum object as a type
type Application = { [Key in ApplicationKeys]: ApplicationProps<Key> }
// define your props with a key so typescript can differentiate between your enums
type ApplicationProps<T> = {
  key: T,
  propA: boolean;
  propB: number;
  propC: string;
}
// the actual enum definition
const application: Application = {
  ELECTRIC: { key: "ELECTRIC", propA: true, propB: 11, propC: "eee" },
  HYDRAULIC: { key: "HYDRAULIC", propA: false, propB: 59, propC: "hhh" },
  PNEUMATIC: { key: "PNEUMATIC", propA: true, propB: 87, propC: "ppp" },
}
// test
let foo = application.ELECTRIC;
let bar: Application["PNEUMATIC"]; 
bar = application.HYDRAULIC; // error: Type 'ApplicationProps<"HYDRAULIC">' is not assignable to type 'ApplicationProps<"ELECTRIC">'. 
bar ===foo // error his condition will always return 'false' since the types have no overlap

The neat part is that if you want to add a new application key you just have to add it to the list and typescript will throw errors for the missing or unhandled key. if you use a proper never assertion in switch or if statements.

I hope you understand my bad english :) CODE

So you can do something like this:


// all application keys will be defined here
// as const is needed here so typescript won't start widening the type to string[]
const applications = ["ELECTRIC", "HYDRAULIC", "PNEUMATIC"] as const

// get all keys as a Union Type
type ApplicationKeys = typeof applications[number]
// create your enum object as a type
type Application = { [Key in ApplicationKeys]: ApplicationProps<Key> }
// define your props with a key so typescript can differentiate between your enums
type ApplicationProps<T> = {
  key: T,
  propA: boolean;
  propB: number;
  propC: string;
}
// the actual enum definition
const application: Application = {
  ELECTRIC: { key: "ELECTRIC", propA: true, propB: 11, propC: "eee" },
  HYDRAULIC: { key: "HYDRAULIC", propA: false, propB: 59, propC: "hhh" },
  PNEUMATIC: { key: "PNEUMATIC", propA: true, propB: 87, propC: "ppp" },
}
// test
let foo = application.ELECTRIC;
let bar: Application["PNEUMATIC"]; 
bar = application.HYDRAULIC; // error: Type 'ApplicationProps<"HYDRAULIC">' is not assignable to type 'ApplicationProps<"ELECTRIC">'. 
bar ===foo // error his condition will always return 'false' since the types have no overlap

The neat part is that if you want to add a new application key you just have to add it to the list and typescript will throw errors for the missing or unhandled key in a switch or if statement(exhaustive switch block)[https://stackoverflow.com/questions/39419170/how-do-i-check-that-a-switch-block-is-exhaustive-in-typescript].

I hope you understand my bad english :) CODE

Source Link
Filly
  • 439
  • 3
  • 9

So you can do something like this:


// all application keys will be defined here
// as const is needed here so typescript won't start widening the type to string[]
const applications = ["ELECTRIC", "HYDRAULIC", "PNEUMATIC"] as const

// get all keys as a Union Type
type ApplicationKeys = typeof applications[number]
// create your enum object as a type
type Application = { [Key in ApplicationKeys]: ApplicationProps<Key> }
// define your props with a key so typescript can differentiate between your enums
type ApplicationProps<T> = {
  key: T,
  propA: boolean;
  propB: number;
  propC: string;
}
// the actual enum definition
const application: Application = {
  ELECTRIC: { key: "ELECTRIC", propA: true, propB: 11, propC: "eee" },
  HYDRAULIC: { key: "HYDRAULIC", propA: false, propB: 59, propC: "hhh" },
  PNEUMATIC: { key: "PNEUMATIC", propA: true, propB: 87, propC: "ppp" },
}
// test
let foo = application.ELECTRIC;
let bar: Application["PNEUMATIC"]; 
bar = application.HYDRAULIC; // error: Type 'ApplicationProps<"HYDRAULIC">' is not assignable to type 'ApplicationProps<"ELECTRIC">'. 
bar ===foo // error his condition will always return 'false' since the types have no overlap

The neat part is that if you want to add a new application key you just have to add it to the list and typescript will throw errors for the missing or unhandled key. if you use a proper never assertion in switch or if statements.

I hope you understand my bad english :) CODE