Skip to content

Commit 4aa61db

Browse files
committed
test: added tests for ValidKeys
1 parent eef20a5 commit 4aa61db

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

test/types/objects.test-d.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
import { describe, expectTypeOf, it } from 'vitest';
22

3-
import type { PathValue, GetValueForKey } from '../../src/types/objects.js';
3+
import type { PathValue, GetValueForKey, ValidKeys } from '../../src/types/objects.js';
4+
5+
describe('ValidKeys', () => {
6+
it('Returns valid keys for an object', () => {
7+
const user = {
8+
name: 'Test Testerson',
9+
[Symbol('age')]: 42,
10+
job: {
11+
title: 'Tester',
12+
},
13+
};
14+
15+
expectTypeOf<ValidKeys<typeof user>>().toEqualTypeOf<'name' | 'job'>();
16+
expectTypeOf<ValidKeys<object>>().toEqualTypeOf<never>();
17+
});
18+
19+
it('Returns valid keys for an array', () => {
20+
const tuple = ['a', 'b', 'c'] as const;
21+
22+
type SampleTuple = [a: string, b: string, c: string];
23+
24+
expectTypeOf<ValidKeys<string[]>>().toEqualTypeOf<number | 'length'>();
25+
26+
expectTypeOf<ValidKeys<never[]>>().toEqualTypeOf<'length'>();
27+
28+
expectTypeOf<ValidKeys<SampleTuple>>().toEqualTypeOf<number | '0' | '1' | '2' | 'length'>();
29+
30+
expectTypeOf<ValidKeys<typeof tuple>>().toEqualTypeOf<number | '0' | '1' | '2' | 'length'>();
31+
});
32+
});
433

534
describe('GetValueForKey', () => {
635
it('Gets the value for a key in an object', () => {
@@ -12,8 +41,8 @@ describe('GetValueForKey', () => {
1241
expectTypeOf<GetValueForKey<[1, 2, 3], 0>>().toEqualTypeOf<1>();
1342
expectTypeOf<GetValueForKey<[1, 2, 3], 4>>().toEqualTypeOf<undefined>();
1443

15-
expectTypeOf<GetValueForKey<string[], '0'>>().toEqualTypeOf<string>();
16-
expectTypeOf<GetValueForKey<string[], 0>>().toEqualTypeOf<string>();
44+
expectTypeOf<GetValueForKey<string[], '0'>>().toEqualTypeOf<string | undefined>();
45+
expectTypeOf<GetValueForKey<string[], 0>>().toEqualTypeOf<string | undefined>();
1746
});
1847
});
1948

@@ -36,7 +65,7 @@ describe('PathValue', () => {
3665

3766
expectTypeOf<PathValue<typeof input, 'name.first'>>().toEqualTypeOf<string>();
3867
expectTypeOf<PathValue<typeof input, 'age'>>().toEqualTypeOf<number>();
39-
expectTypeOf<PathValue<typeof input, 'role.0.job.title'>>().toEqualTypeOf<string>();
68+
expectTypeOf<PathValue<typeof input, 'role.0.job.title'>>().toEqualTypeOf<string | undefined>();
4069
});
4170

4271
it('Gets the value from a const Type using path dot notation', () => {

0 commit comments

Comments
 (0)