microsoft / microsoft/TypeScript
Change default inference from arrays to tuples and primitives to literals
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
I am working on functional project now and I'm getting a lot of errors related to too wide type inference for values. It looks like
function test(a: { data: 1 | 2 }) { return a }
const a = { data: 2 }
// Types of property 'data' are incompatible.
// Type 'number' is not assignable to type '2'.ts(2345)
const b = test(a)
There're really many errors like this, and the main problem is that you can convert type A to type B, but not vice versa.
type A = { data: 2 }
type B = { data: number }
So, I ask you to add a new compiler option (to not break existing code) that will make type checker infer types as narrow as possible. It will be very helpful for functional programming.
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 reviewing the TypeScript type checker and compiler-option documentation, using the issue's array, primitive, and literal-inference examples as the expected behavior. Define the option's scope and compatibility rules first; done means the requested narrow inference works without changing existing behavior when the option is disabled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100