microsoft / microsoft/TypeScript

Allow narrowing type based on others properties when creating a new object

Open
#37,224 3 comments 1 reaction 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

Search Terms

  • narrowing type

Suggestion

Currently, TypeScript isn't narrowing the type based on others properties that is defined while creating a new object. For example. Let's say the we have the following function:

type TParams = { kind: 'name', value: string } | { kind: 'age', value: number }
const example = (params: TParams) => {
  ...
}

So we can call it using codes like:

example({ kind: 'name', value: 'macabeus' })
example({ kind: 'age', value: 23 })

But isn't possible to compile code like:

let nameOrAge: 'name' | 'age' = 'name'

example({
    kind: nameOrAge,
    value: (nameOrAge === 'name' ? 'macabeus' : 23),
})

Playground

but we know that, if nameOrAge is 'name', the property value will be a string, otherwise nameOrAge will be 'age' and value will be 23 - that is correct.
I think that would be useful if TS could check this behaviour.

Use Cases

It would be useful to reduce the amount of code on situations like described on the above section.
Currently, is necessary to write more lines to have the same behaviour:

let nameOrAge: 'name' | 'age' = 'name'

if (nameOrAge === 'name') {    
    example({
        kind: nameOrAge,
        value: 'macabeus',
    })
} else {
    example({
        kind: nameOrAge,
        value: 23,
    })
}

Playground

Related

I know that there are a lot of issues saying about narrowing types based on variables, such as:

  • that about narrowing on arrays values
  • that about narrowing when calling a function

But here I'm saying only about when is creating a new object, and as far as I know is a simpler case if compared on these others issues, since doesn't need to track the entire code base.
On the case that I'm saying, TS just need to check the others properties defined on the same object and if the variable name already is used. For example, TS doesn't need to change its behaviour on this case:

example({
  kind: nameOrAge,
  value: otherVariabledDeclaredOnOtherScope ? 'macabeus' : 23,
}) // still should fail

Probably it isn't the best solution, but I think that it follow the Pareto efficiency.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 running the linked TypeScript Playground reproduction and comparing it with the working branch example. Investigate how object literals are checked against discriminated unions; done means the proposed kind and value combination is accepted when correlated and unrelated variables still fail, with regression coverage added in the compiler test suite.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.