You can define primitive type aliases (e.g. string identifiers) in TypeScript that prevent you from using the wrong type using unique symbols:

type userId = string & {readonly id: unique symbol};
type projectId = string & {readonly id: unique symbol};

// A string can't just be assigned as a userId anymore, but this works.
const myUserId: userId = "u001" as userId;

// TS2322: Type 'userId' is not assignable to type 'projectId'. ๐ŸŽ‰
const myProjectId: projectId = myUserId;
Read more

You can define a "collection" of TypeScript types using enums and interfaces, like this:

enum MyList {
  A,
  B,
  C
}

interface MyData {
  [MyList.A]: {color: string, enabled: boolean},
  [MyList.B]: string,
  [MyList.C]: any
}
Read more
Subscribe to TypeScript
Mastodon Mastodon