microsoft / microsoft/TypeScript

Type inference in index type assignment is skipped

Open
#37,874 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.8.3

Search Terms: index indexed type assignment initialize initializer initialization inference object narrow narrowing assertion

Code

type One = { name: string; };
type Many = Array<One>;
type OneOrMany = One | Many;

var one: OneOrMany = { name: "sam" };
one.name = "pat"
var many: OneOrMany = [{ name: "sam" },{ name: "ash" }];
many[0].name = "pat"
// all is well here

interface OOMDict { [key: string]: OneOrMany; }
var oomdict2: OOMDict = {};
oomdict2.one = { name: "sam" };
oomdict2.one.name = "pat";
oomdict2.many = [{ name: "sam" }, { name: "ash" }];
oomdict2.many[0].name = "pat";
// all is well here

// testing resolution within an index type initializer
var oomdict: OOMDict = { one: { name: "sam" }, many: [{ name: "sam" }, { name: "ash" }] };
oomdict.one.name = "pat";
// ERROR: Property 'name' does not exist on type 'OneOrMany'.
// ERROR: Property 'name' does not exist on type 'Many'.(2339)
oomdict.many[0].name = "pat";
// ERROR: Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'OneOrMany'.
// ERROR: Property '0' does not exist on type 'OneOrMany'.(7053)

Expected behavior: Initialization / assignment of an OOMDict object effects type inference on its OneOrMany values identically to initialization or assignment of a OneOrMany variable.

Actual behavior: Values in an OOMDict either do not narrow based on their contents and type inference (oomdict.many does not narrow from OneOrMany to Many) or they narrow to the wrong specific type (oomdict.one narrows from OneOfMany to Many instead of One).

Playground Link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=56&pc=1#code/PTAECMEMGcEsGNQBcCeAHAptAUKzoB5AOw1AF5QBvUIyAWwwC5RokAnWIgcwG5QBfHrnSkAspCIpyoAIJs2kFAB5iGAHxC8pVQTbjJ01aAA+ofSiHYQyLEk5dQbLAHsANgFc7zoqGcAzZAALbRJdc2QRCBgMABNfH3hvJAwiJBwAN0g2eKZCU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xXG6WiGGxoB1AWWpjPP0A2ug7VvWza2Z25xBv29XjqmulZvli6sa2NXzeBZUAHYxQAVke96wBkYy8YAWgVjAaymcNYA8NKzwhjaLREwI1zw9yhUPFkJFm4ILnAAArKOkFU2LaMezTq1rKh6zhFgQbYSMq+04pdKocz4Msk4TNAMybIM7uO4H1D0PJRy-Bi5UMcaFMpG9fj3CbjBtSoaCZ3Civk1TByKEX5ft5QClWzAVCkckGp89IE7oG1xpglcNAaaQR6ooZljn1fdTX7ppjpSa1iFpFykFDLMNgOVfj-EBBaWW1ohx+DKjWISPhJ7ZEyPzGIDRkESRuHcVW6BrTYUIFWbSfIMCuD8Ngf08BXBZFIAgogSCtjoO+g0DAypN7xhbjWbUzB0jtBiNgfYbt1IABYdJdD0p3QeS4jK937piLusirLD0UYSOyGFHIsMwckDhYjtSWCAA

Related Issues: https://github.com/Microsoft/TypeScript/issues/1706 https://github.com/microsoft/TypeScript/issues/5089

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 with the minimal reproduction in the issue or its Playground link, using the TypeScript 3.8.3 behavior as the baseline. Compare narrowing for direct OneOrMany initialization with values inside the OOMDict index-type initializer. Done means the two OOMDict property assignments narrow like the direct assignments and no reported type errors remain.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.