Skip to content

Instantly share code, notes, and snippets.

@beetcb
Last active August 18, 2023 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beetcb/c3db748573ad4d5cca85bc5184ead1bc to your computer and use it in GitHub Desktop.
Save beetcb/c3db748573ad4d5cca85bc5184ead1bc to your computer and use it in GitHub Desktop.
Exclude number type from typescript `keyof` string indexed signature
// see -> https://www.typescriptlang.org/docs/handbook/2/keyof-types.html
type ExcludeUnexpectedNumIdxSigKeyof<T> = keyof T extends number
? keyof T
: Extract<keyof T, string>;
type MapishMixed = { [k: number | string]: boolean };
type M = ExcludeUnexpectedNumIdxSigKeyof<MapishMixed>;
// ^? M will be of type string | number
type MapishNumber = { [k: number]: boolean };
type N = ExcludeUnexpectedNumIdxSigKeyof<MapishNumber>;
// ^? N will be of type number
type MapishString = { [k: string]: boolean };
type O = ExcludeUnexpectedNumIdxSigKeyof<MapishString>;
// ^? O will be of type string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment