Last active
August 18, 2023 09:30
-
-
Save beetcb/c3db748573ad4d5cca85bc5184ead1bc to your computer and use it in GitHub Desktop.
Exclude number type from typescript `keyof` string indexed signature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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