Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Created September 22, 2019 22:13
Show Gist options
  • Save karol-majewski/f0c0d1986add0b2a8b04cf9fe9506ed8 to your computer and use it in GitHub Desktop.
Save karol-majewski/f0c0d1986add0b2a8b04cf9fe9506ed8 to your computer and use it in GitHub Desktop.
Homogeneous arrays in TypeScript (+ check if a type is a union)
type Singleton = string;
type Union = string | number;
type DistributedKeyOf<T> =
T extends any
? keyof T
: never;
type DistributedValueOf<T> = T[DistributedKeyOf<T>];
type IsSingleton<T> =
unknown extends DistributedValueOf<T>
? false
: true;
type T1 = IsSingleton<Singleton> // $ExpectType true
type T2 = IsSingleton<Union> // $ExpectType false
class HomogeneousArray<T> extends Array<IsSingleton<T> extends true ? T : never> {}
const numbers: HomogeneousArray<number> = [1, 2, 3]; // ✔
const mixed: HomogeneousArray<string | number> = [1, '2', 3]; // ✘
@karol-majewski
Copy link
Author

Nice! Your solution is simpler and gives a better error message, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment