Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dfd5965

Browse files
committedJun 7, 2025··
Add 'as const' to tests for readonly keyword
1 parent a910dcb commit dfd5965

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed
 

‎test-d/union-to-enum.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,77 @@ import {expectType} from 'tsd';
22
import type {UnionToEnum} from '../index.d.ts';
33

44
// Union input
5-
expectType<UnionToEnum<'b' | 'a' | 'd' | 'c'>>({b: 'b', a: 'a', d: 'd', c: 'c'});
6-
expectType<UnionToEnum<3 | 2 | 4 | 1>>({1: 1, 2: 2, 3: 3, 4: 4});
5+
expectType<UnionToEnum<'b' | 'a' | 'd' | 'c'>>({b: 'b', a: 'a', d: 'd', c: 'c'} as const);
6+
expectType<UnionToEnum<3 | 2 | 4 | 1>>({1: 1, 2: 2, 3: 3, 4: 4} as const);
77

88
// Tuple input
9-
expectType<UnionToEnum<['One', 'Two']>>({One: 'One', Two: 'Two'});
10-
expectType<UnionToEnum<['X', 'Y', 'Z'], true>>({X: 1, Y: 2, Z: 3});
9+
expectType<UnionToEnum<['One', 'Two']>>({One: 'One', Two: 'Two'} as const);
10+
expectType<UnionToEnum<['X', 'Y', 'Z'], true>>({X: 1, Y: 2, Z: 3} as const);
1111

1212
// Single element tuple
13-
expectType<UnionToEnum<['Only']>>({Only: 'Only'});
14-
expectType<UnionToEnum<'Only', true>>({Only: 1});
13+
expectType<UnionToEnum<['Only']>>({Only: 'Only'} as const);
14+
expectType<UnionToEnum<'Only', true>>({Only: 1} as const);
1515

1616
// Tuple with numeric keys
17-
expectType<UnionToEnum<[1, 2, 3]>>({1: 1, 2: 2, 3: 3});
18-
expectType<UnionToEnum<[1, 2, 3], true, {startIndex: 10}>>({1: 10, 2: 11, 3: 12});
17+
expectType<UnionToEnum<[1, 2, 3]>>({1: 1, 2: 2, 3: 3} as const);
18+
expectType<UnionToEnum<[1, 2, 3], true, {startIndex: 10}>>({1: 10, 2: 11, 3: 12} as const);
1919

2020
// Mixed keys
21-
expectType<UnionToEnum<['a', 1, 'b']>>({a: 'a', 1: 1, b: 'b'});
22-
expectType<UnionToEnum<['a', 1, 'b'], true, {startIndex: 0}>>({a: 0, 1: 1, b: 2});
21+
expectType<UnionToEnum<['a', 1, 'b']>>({a: 'a', 1: 1, b: 'b'} as const);
22+
expectType<UnionToEnum<['a', 1, 'b'], true, {startIndex: 0}>>({a: 0, 1: 1, b: 2} as const);
2323

2424
// Literal const arrays
2525
const buttons = ['Play', 'Pause', 'Stop'] as const;
2626

27-
expectType<UnionToEnum<typeof buttons>>({Play: 'Play', Pause: 'Pause', Stop: 'Stop'});
28-
expectType<UnionToEnum<typeof buttons, true, {startIndex: 0}>>({Play: 0, Pause: 1, Stop: 2});
27+
expectType<UnionToEnum<typeof buttons>>({Play: 'Play', Pause: 'Pause', Stop: 'Stop'} as const);
28+
expectType<UnionToEnum<typeof buttons, true, {startIndex: 0}>>({Play: 0, Pause: 1, Stop: 2} as const);
2929

3030
// Symbol keys
3131
declare const sym1: unique symbol;
3232
declare const sym2: unique symbol;
3333

34-
expectType<UnionToEnum<typeof sym1 | typeof sym2>>({[sym1]: sym1, [sym2]: sym2});
35-
expectType<UnionToEnum<[typeof sym1, typeof sym2]>>({[sym1]: sym1, [sym2]: sym2});
34+
expectType<UnionToEnum<typeof sym1 | typeof sym2>>({[sym1]: sym1, [sym2]: sym2} as const);
35+
expectType<UnionToEnum<[typeof sym1, typeof sym2]>>({[sym1]: sym1, [sym2]: sym2} as const);
3636

3737
// Unordered union with numeric flag
38-
expectType<UnionToEnum<'left' | 'right' | 'up' | 'down', true>>({left: 1, right: 2, up: 3, down: 4});
38+
expectType<UnionToEnum<'left' | 'right' | 'up' | 'down', true>>({left: 1, right: 2, up: 3, down: 4} as const);
3939

4040
// Large union
4141
type BigUnion = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g';
42-
expectType<UnionToEnum<BigUnion>>({a: 'a', b: 'b', c: 'c', d: 'd', e: 'e', f: 'f', g: 'g'});
42+
expectType<UnionToEnum<BigUnion>>({a: 'a', b: 'b', c: 'c', d: 'd', e: 'e', f: 'f', g: 'g'} as const);
4343

4444
// Non-literal input fallback
4545
expectType<UnionToEnum<string>>({} as Record<string, string>);
4646
expectType<UnionToEnum<number>>({} as Record<number, number>);
4747
expectType<UnionToEnum<symbol>>({} as Record<symbol, symbol>);
4848

49-
expectType<UnionToEnum<string[]>>({});
50-
expectType<UnionToEnum<number[]>>({});
51-
expectType<UnionToEnum<symbol[]>>({});
49+
expectType<UnionToEnum<string[]>>({} as const);
50+
expectType<UnionToEnum<number[]>>({} as const);
51+
expectType<UnionToEnum<symbol[]>>({} as const);
5252

5353
// `never` / `any`
54-
expectType<UnionToEnum<never>>({});
55-
expectType<UnionToEnum<any>>({});
54+
expectType<UnionToEnum<never>>({} as const);
55+
expectType<UnionToEnum<any>>({} as const);
5656

5757
// CamelCase
5858
const level = ['DEBUG', 'INFO', 'ERROR', 'WARNING'] as const;
5959
expectType<UnionToEnum<typeof buttons, false, {camelCase: true}>>({
6060
play: 'Play',
6161
pause: 'Pause',
6262
stop: 'Stop',
63-
});
63+
} as const);
6464
expectType<UnionToEnum<typeof level, false, {camelCase: true}>>({
6565
debug: 'DEBUG',
6666
info: 'INFO',
6767
error: 'ERROR',
6868
warning: 'WARNING',
69-
});
69+
} as const);
7070
expectType<UnionToEnum<typeof level, true, {camelCase: true}>>({
7171
debug: 1,
7272
info: 2,
7373
error: 3,
7474
warning: 4,
75-
});
75+
} as const);
7676

7777
// Dynamic Enum
7878
const verb = ['write', 'read', 'delete'] as const;
@@ -95,4 +95,4 @@ expectType<typeof Template>({
9595
deleteFile: 'delete_file',
9696
deleteFolder: 'delete_folder',
9797
deleteLink: 'delete_link',
98-
});
98+
} as const);

0 commit comments

Comments
 (0)
Please sign in to comment.