microsoft / microsoft/TypeScript
Types that depend on constants are not narrowing correctly
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
"ternary narrowing", "ternary constant", "constant narrowing", "constant dependent"
🕗 Version & Regression Information
- This is the behavior in every version I tried (including nightly), and I reviewed the FAQ.
⏯ Playground Link
💻 Code
const BOOL = true as const
type T_True = { x: string, T: string }
type T_False = { x: string, F: string }
type T = (typeof BOOL) extends true ? T_True : T_False
function f(): T {
const varT: T_True = { x: 'varT', T: 'good' }
const varF: T_False = { x: 'varF', F: 'okay' }
return BOOL ? varT : varF
}
🙁 Actual behavior
Typechecking fails with the error Type 'T_True | T_False' is not assignable to type 'T_True'. Property 'T' is missing in type 'T_False' but required in type 'T_True'. on the return statement.
🙂 Expected behavior
Typechecking should pass, since the const BOOL = true as const should mark the false ternary case as unreachable.
I also tried making T generic with respect to BOOL, and splitting the ternary operator into an if/else, but the same error persisted regardless.
Additional information about the issue
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked TypeScript Playground with the supplied BOOL, conditional type, and ternary example to reproduce the return-statement error. Trace the type-checking behavior for constant conditions; done means the example type-checks with the false branch treated as unreachable.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100