Skip to content

Commit e492c9c

Browse files
authored
StringKeyOf: Rename to KeyAsString (#1182)
1 parent 6d38d72 commit e492c9c

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export type {LessThan} from './source/less-than.d.ts';
9494
export type {LessThanOrEqual} from './source/less-than-or-equal.d.ts';
9595
export type {Sum} from './source/sum.d.ts';
9696
export type {Subtract} from './source/subtract.d.ts';
97-
export type {StringKeyOf} from './source/string-key-of.d.ts';
97+
export type {KeyAsString} from './source/key-as-string.d.ts';
9898
export type {Exact} from './source/exact.d.ts';
9999
export type {ReadonlyTuple} from './source/readonly-tuple.d.ts';
100100
export type {OptionalKeysOf} from './source/optional-keys-of.d.ts';

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Click the type names for complete docs.
148148
- [`Simplify`](source/simplify.d.ts) - Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
149149
- [`SimplifyDeep`](source/simplify-deep.d.ts) - Deeply simplifies an object type.
150150
- [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
151-
- [`StringKeyOf`](source/string-key-of.d.ts) - Get keys of the given type as strings.
151+
- [`KeyAsString`](source/key-as-string.d.ts) - Get keys of the given type as strings.
152152
- [`Schema`](source/schema.d.ts) - Create a deep version of another object type where property values are recursively replaced into a given value type.
153153
- [`Exact`](source/exact.d.ts) - Create a type that does not allow extra properties.
154154
- [`OptionalKeysOf`](source/optional-keys-of.d.ts) - Extract all optional keys from the given type.

source/get.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {ApplyDefaultOptions, ToString} from './internal/index.d.ts';
22
import type {LiteralStringUnion} from './literal-union.d.ts';
33
import type {Paths} from './paths.d.ts';
44
import type {Split} from './split.d.ts';
5-
import type {StringKeyOf} from './string-key-of.d.ts';
5+
import type {KeyAsString} from './key-as-string.d.ts';
66
import type {DigitCharacter} from './characters.d.ts';
77

88
type GetOptions = {
@@ -112,7 +112,7 @@ type WithStringsKeys = keyof WithStrings;
112112
```
113113
*/
114114
type WithStringKeys<BaseType> = {
115-
[Key in StringKeyOf<BaseType>]: UncheckedIndex<BaseType, Key>
115+
[Key in KeyAsString<BaseType>]: UncheckedIndex<BaseType, Key>
116116
};
117117

118118
/**

source/string-key-of.d.ts renamed to source/key-as-string.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ Use-cases:
99
1010
@example
1111
```
12-
import type {StringKeyOf} from 'type-fest';
12+
import type {KeyAsString} from 'type-fest';
1313
1414
type Foo = {
15-
1: number,
16-
stringKey: string,
15+
1: number;
16+
stringKey: string;
1717
};
1818
19-
type StringKeysOfFoo = StringKeyOf<Foo>;
19+
type StringKeysOfFoo = KeyAsString<Foo>;
2020
//=> '1' | 'stringKey'
2121
```
2222
2323
@category Object
2424
*/
25-
export type StringKeyOf<BaseType> = `${Extract<keyof BaseType, string | number>}`;
25+
export type KeyAsString<BaseType> = `${Extract<keyof BaseType, string | number>}`;

test-d/key-as-string.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {expectType} from 'tsd';
2+
import type {KeyAsString} from '../source/key-as-string.d.ts';
3+
4+
declare const foo: KeyAsString<{
5+
1: number;
6+
stringKey: string;
7+
}>;
8+
9+
expectType<'1' | 'stringKey'>(foo);
10+
11+
declare const sym: unique symbol;
12+
expectType<'1' | 'foo'>({} as KeyAsString<{[sym]: unknown; 1: number; foo: string}>); // Ignores symbol keys
13+
14+
// Index signatures
15+
expectType<string>({} as KeyAsString<{[x: string]: unknown}>);
16+
expectType<`${number}`>({} as KeyAsString<{[x: number]: unknown}>);
17+
expectType<`foo${string}` | `${number}`>({} as KeyAsString<{[x: `foo${string}`]: unknown; [y: number]: unknown}>);
18+
expectType<string>({} as KeyAsString<{[x: string]: unknown; [y: symbol]: unknown}>);
19+
expectType<never>({} as KeyAsString<{[x: symbol]: unknown}>);
20+
21+
// Unions
22+
expectType<'1'>({} as KeyAsString<{1: number; foo: string} | {1: number; bar: string}>); // Only grabs the common keys, just like `keyof`.
23+
expectType<never>({} as KeyAsString<{foo: string} | {bar: string}>);
24+
25+
// Boundary cases
26+
expectType<string>({} as KeyAsString<any>);
27+
expectType<string>({} as KeyAsString<never>);

test-d/string-key-of.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)