Repository: TypeScript
Issue: Allow narrowing type based on others properties when creating a new object
Labels: Suggestion,Awaiting More Feedback

## 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:

```ts
type TParams = { kind: 'name', value: string } | { kind: 'age', value: number }
const example = (params: TParams) => {
  ...
}
```

So we can call it using codes like:

```ts
example({ kind: 'name', value: 'macabeus' })
example({ kind: 'age', value: 23 })
```

But isn't possible to compile code like:

```ts
let nameOrAge: 'name' | 'age' = 'name'

example({
    kind: nameOrAge,
    value: (nameOrAge === 'name' ? 'macabeus' : 23),
})
```
[_Playground_](https://www.typescriptlang.org/play/?ssl=18&ssc=2&pln=6&pc=1#code/C4TwDgpgBAKgCgQwE4ILYGcoF4oG8oDWAlgHYAmAXFAOQloTUA0UAbggDYCuEV6wSpAOZQAvlAA+eQqUo0EghszZceUEp1QAjCElEBYAFABjAPYk+UCAA80YdtBwAKMMjToq8VxgCU2AHx4hoYiQQb2wGr0APJIAIIKVLT01BJyCik4SagModa29o64hlAl0uRUdNkx8RCMxaXK3FSOlRDVCthYma0pAPw0qAhGCNqc6ClUAEwAzN51BiLehkA)

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:

```ts
let nameOrAge: 'name' | 'age' = 'name'

if (nameOrAge === 'name') {    
    example({
        kind: nameOrAge,
        value: 'macabeus',
    })
} else {
    example({
        kind: nameOrAge,
        value: 23,
    })
}
```
[_Playground_](https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=19&pc=1#code/C4TwDgpgBAKgCgQwE4ILYGcoF4oG8oDWAlgHYAmAXFAOQloTUA0UAbggDYCuEV6wSpAOZQAvlAA+eQqUo0EghszZceUEp1QAjCElEBYAFABjAPYk+UCAA80YdtBwAKMMjToq8VxgCU2AHx4hoYiQQb2wGr0APJIAIIKVLT01BJyCik4SagMoUQAZlCOdNkx8Q5YmcUMvvhQdYZ1dda29o64DY2dxORUVaUKjB2ddcrciagIRgjanOhMQ6LewZbs6NDtBp3NqHYQbQtdMr3RcQMHjaOqAEwAzIObjSJLBiEGQA)

## Related

I know that there are a lot of issues saying about narrowing types based on variables, such as:
- [that](https://github.com/microsoft/TypeScript/issues/28981) about narrowing on arrays values
- [that](https://github.com/microsoft/TypeScript/issues/12184) 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:

```ts
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:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).