Skip to content

fix: preserve literal types in Infer by using const type parameter#7

Merged
mhweiner merged 1 commit intologfoxai:mainfrom
technicalcandidate-stack:fix/literal-const-inference
Jan 30, 2026
Merged

fix: preserve literal types in Infer by using const type parameter#7
mhweiner merged 1 commit intologfoxai:mainfrom
technicalcandidate-stack:fix/literal-const-inference

Conversation

@technicalcandidate-stack
Copy link
Copy Markdown
Contributor

Use TypeScript 5.0's const type parameter modifier to preserve literal types through inference. Previously, literal('text') would infer as string instead of 'text', breaking discriminated union narrowing.

Before:
const v = literal('text');
type T = Infer; // string

After:
const v = literal('text');
type T = Infer; // 'text'

This removes the need for callers to use as const at call sites.

- Change literal<T extends any> to literal<const T> for proper type inference
- Add type-level tests to verify literal types are preserved
- Add TypeScript 5.0+ requirement to README
- Add optional peerDependency on TypeScript >=5.0.0

The const type parameter modifier (TS 5.0+) prevents type widening,
so Infer<typeof literal('text')> now correctly produces 'text' instead of string.
@mhweiner mhweiner merged commit cafa479 into logfoxai:main Jan 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants