Skip to content

Commit fb72bd9

Browse files
author
Ben Grynhaus
committed
Added KnownKeys mapped type
1 parent c5c6e55 commit fb72bd9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Get the known keys (i.e. no index signature) of T.
3+
*
4+
* @example
5+
```typescript
6+
interface Options {
7+
key: string;
8+
title: string;
9+
[dataProperty: string]: string | number;
10+
}
11+
12+
type KnownKeysOfOptions = KnownKeys<Options>; // 'key' | 'title';
13+
// (vs. type KeysOfOptions = keyof Options // string | number;)
14+
```
15+
16+
* Taken from https://stackoverflow.com/questions/51465182/typescript-remove-index-signature-using-mapped-types
17+
*/
18+
export type KnownKeys<T> = { [K in keyof T]: string extends K ? never : number extends K ? never : K } extends {
19+
[_ in keyof T]: infer U
20+
}
21+
? U
22+
: never;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
export * from './known-keys';
45
export * from './omit';
56
export * from './StringMap';

0 commit comments

Comments
 (0)