Skip to content

Commit

Permalink
ObjectValue: Support any kind of number index (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa committed Nov 15, 2023
1 parent e58127a commit 972815c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion source/internal.d.ts
Expand Up @@ -155,7 +155,16 @@ Extract the object field type if T is an object and K is a key of T, return `nev
It creates a type-safe way to access the member type of `unknown` type.
*/
export type ObjectValue<T, K> = K extends keyof T ? T[K] : never;
export type ObjectValue<T, K> =
K extends keyof T
? T[K]
: ToString<K> extends keyof T
? T[ToString<K>]
: K extends `${infer NumberK extends number}`
? NumberK extends keyof T
? T[NumberK]
: never
: never;

/**
Returns a boolean for whether the string is lowercased.
Expand Down
20 changes: 20 additions & 0 deletions test-d/internal/object-value.ts
@@ -0,0 +1,20 @@
import {expectType} from 'tsd';
import {type ObjectValue} from '../../source/internal';

type ObjectT = {
string: string;
0: number;
'1': number;
};

declare const normal: ObjectValue<ObjectT, 'string'>;
expectType<string>(normal);

declare const test0: ObjectValue<ObjectT, 0>;
expectType<number>(test0);
declare const teststring0: ObjectValue<ObjectT, '0'>;
expectType<number>(teststring0);
declare const test1: ObjectValue<ObjectT, 1>;
expectType<number>(test1);
declare const teststring1: ObjectValue<ObjectT, '1'>;
expectType<number>(teststring1);

0 comments on commit 972815c

Please sign in to comment.