microsoft / microsoft/TypeScript

Change default inference from arrays to tuples and primitives to literals

Open
#38,831 18 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.