microsoft / microsoft/TypeScript
Suggestion: short-circuit type inference for ternary operators when both branches have the same type
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
From https://github.com/squidfunk/typescript-issue-38339-repro (courtesy of @squidfunk)
TypeScript Version: 3.9.1-rc
Search Terms:
Subtype reduction, ternary
Code
import { JSX } from "preact"
type IntrinsicElements = JSX.IntrinsicElements[keyof JSX.IntrinsicElements]
export type IntrinsicElements2<
T = IntrinsicElements
> = {
[K in keyof T]: K extends "accept"
? T[K] | [string]
: T[K]
}
export type IntrinsicElements3<
T = IntrinsicElements2
> = {
[K in keyof T]: K extends "class"
? T[K] | [boolean]
: T[K]
}
export type IntrinsicElements4<
T = IntrinsicElements3
> = {
[K in keyof T]: K extends "id"
? T[K] | [number]
: T[K]
}
export function foo(): IntrinsicElements4 | undefined {
let t: IntrinsicElements4 | undefined
// this takes 2 seconds to check
return t
// this takes 10 seconds to check
// return true ? t : t
}
Expected behavior: return t and return true ? t : t check in a comparable amount of time
Actual behavior: the ternary is much slower
Related Issues: #38339
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 with the linked reproduction and Playground example, comparing the standalone return with the ternary return in the provided TypeScript code. Investigate the type-checking path for ternary operators and the related issue #38339; done means both forms check in a comparable amount of time without changing the expected types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100