Skip to content

Commit 20d04ac

Browse files
committed
Fix enumeration type inference for inline members
1 parent 1f29613 commit 20d04ac

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Versioning].
1515
- Added a `Declaration<T>` type that can be used to explicitly type
1616
declarations.
1717

18+
### Fixed
19+
20+
- Enumeration declarations can now infer the value type when the `members`
21+
argument is passed as an object without needing to use `as const`.
22+
1823
## [v0.12.0] - 2024-11-08
1924

2025
[v0.12.0]: https://github.com/ezzatron/austenite/releases/tag/v0.12.0

src/declaration/enumeration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type Options<T> = DeclarationOptions<T> &
2727
DeclarationConstraintOptions<T> &
2828
DeclarationExampleOptions<T>;
2929

30-
export function enumeration<T, O extends Options<T>>(
30+
export function enumeration<const T, O extends Options<T>>(
3131
name: string,
3232
description: string,
3333
members: Members<T>,

test/suite/declaration/enumeration.spec-d.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,41 @@ describe("Enumeration declarations", () => {
8282
});
8383
});
8484

85+
describe('when declared with inline members that are not defined with "as const"', () => {
86+
it("infers the declaration type from the members", () => {
87+
const required = enumeration("AUSTENITE_ENUMERATION", "<description>", {
88+
"<member-0>": {
89+
value: 0,
90+
description: "member 0",
91+
},
92+
"<member-1>": {
93+
value: 1,
94+
description: "member 1",
95+
},
96+
});
97+
const optional = enumeration(
98+
"AUSTENITE_ENUMERATION",
99+
"<description>",
100+
{
101+
"<member-0>": {
102+
value: 0,
103+
description: "member 0",
104+
},
105+
"<member-1>": {
106+
value: 1,
107+
description: "member 1",
108+
},
109+
},
110+
{
111+
default: undefined,
112+
},
113+
);
114+
115+
expectTypeOf(required).toEqualTypeOf<Declaration<0 | 1>>();
116+
expectTypeOf(optional).toEqualTypeOf<Declaration<0 | 1 | undefined>>();
117+
});
118+
});
119+
85120
describe("when valid options are specified", () => {
86121
it("does not allow unknown options", () => {
87122
const declaration = enumeration(

0 commit comments

Comments
 (0)