microsoft / microsoft/TypeScript

Error not generated for nested generic array

Open
#60,453 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Variance Relationships Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 Search Terms

error, nested, array, order

🕗 Version & Regression Information
  • This changed between versions 4.4.4 and 4.5.5 (from playground testing - all later version exhibit this behaviour).
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.3#code/C4TwDgpgBAKuEEEoF4oDsCuBbARhATgNwBQoks8AQilDgPZ0A2EAhmiWdACIvAtyQYAdzoAeGFAgAPYBDQATAM4VISAD4qIlAHw0A3sShQ6aCAC4oCfPhYhReqMBEXL12+N0BfbSU8d4UDx8AhAwABb4EBDikjJySprqmjr6hsamFlY2dg5OdJluOY4RUQXZHlDelT7EfsTEzMCOEIrAAJZoAObhkRAAotYWQfzwPVHi8Ai6qAZGJuZQANq5zktQesAlC4uO+BjQALqVUAeeRwD055LWdPjKLMrSkADGsvK1JI3NrR3dIgP4Ia8EaCEQTVTTdZpeYWZaOVY7ABmLEYikOlQuVwI+Fu90eUhebw+9UuUAAqmhnnQsFg5E1Ni1oIwOi1HHQoGjoCjGNccXdiKSvrIfl1hHQAAqRIHBUZgkJTVKkuYZJYrfJrACsJwxAqudUFEHpACYWJKFsMQmLwVpIcL2qKRGaSA1Dd97d0ttKQaEttaFTNoSq4XlYetNr1Q1qjp4TmdiaSAMLU2loJp0DD0sJtZTM0xsjlRXm3KCI4vAE1QB6KNqdNAp4Au+kAZhYXstvpCKVQdt+YwghCgpIAymF04x5EX8AAaWgZqDyOistB0BuNt291YW2VieWQ2bpbZqyPauN1IUmtvb61d9cOugD0nY3HpRggKBtREF15tEyV+gAN2gKkaTpCAJ3TVcvmeMIIGeABrCxMFwAgaAAIgeeREVQh8rhRIRbEeG47hnB53yaRRRwwccSSuJMQNTYw5wAGSNAB2fNOSgKlrFgpon3wdIoCYpsjV1clKWTOkhLYysFCgXNWQARgADgAWiNRSOMLblJ2UbjIleV8aKgAB1W44LxWJCTA980Cgf8ABYADpnIcmccDnZcmjYN9GF4FDALuH80EUYggA

💻 Code
type TypeA = number;
type TypeB = boolean;
type DataTypeTwo<T extends TypeA | TypeB> = {
  one: Array<{ two:  Array<T> }>;
};
type DataTypeThree<T extends TypeA | TypeB> = {
  one: Array<{ two: Array<{ three: Array<T> }> }>;
};

let testingThreeErr: DataTypeThree<TypeA> = {
  one: [{ two: [ {three: [ true ] } ]}] // errors as expected
};
let testingTwoErr: DataTypeTwo<TypeA> = {
  one: [{ two: [ false ] }] // errors as expected
};

// Uncomment these lines to see all errors
// let testingTwoPre: DataTypeTwo<TypeA> = {
//   one: [{ two: [ 5 ] }]
// };
// let t2aPre: DataTypeTwo<TypeB> = testingTwoPre;

let testingThree: DataTypeThree<TypeA> = {
  one: [{ two: [ {three: [ 5 ] } ]}]
};
// Comment out this line to see error for t2a assignment
let t3a: DataTypeThree<TypeB> = testingThree; // Should error, but does not

let testingTwo: DataTypeTwo<TypeA> = {
  one: [{ two: [ 5 ] }]
};
let t2a: DataTypeTwo<TypeB> = testingTwo; // errors only if section above commented out

let check: number = "asdf"; // always errors, as it should

// Comment out L27 to see correct error on L32
// Uncomment L27 and lines 18-21 to see all errors correctly

// Works as expected in v4.4.4, but not any later versions

🙁 Actual behavior

No errors on lines 27 and 32 (assigning an incompatible type).

🙂 Expected behavior

Should error on those lines.

Additional information about the issue

The code works (i.e. gives correct errors) in version 4.4.4 (pg).

I assume this is to with depth limits, as it works fine for the two level case (DataTypeTwo) - though if the bad assignment comes after the three level case then even this error is not shown. (i.e. need to comment out line 27 in the example for the bad assignment to the DataTypeTwo variable to correctly error).

Even more oddly, if a bad assignment to a DataTypeTwo variable is made before the bad assignment to a DataTypeThree, the DataTypeThree assignment begins to error correctly. (Uncomment lines 18-21 in the example).

Possibly related to #56291.

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 linked TypeScript Playground and compare the reproduction in 4.4.4 with 5.6.3 or later. Investigate why the nested DataTypeTwo and DataTypeThree assignments affect whether incompatible assignments are reported, using the examples and the related issue #56291 as context. Done means the assignments on lines 27 and 32 reliably produce errors without depending on code order, with regression coverage added in the compiler tests.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.