Skip to content

New error in 3.9: TS 7053 - can't index with number #38102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amcasey opened this issue Apr 22, 2020 · 5 comments · Fixed by #38278
Closed

New error in 3.9: TS 7053 - can't index with number #38102

amcasey opened this issue Apr 22, 2020 · 5 comments · Fixed by #38278
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@amcasey
Copy link
Member

amcasey commented Apr 22, 2020

TypeScript Version: 3.9.0-dev.20200420

Search Terms:

Intersection, Union, 7053

Code

export type TypedArray = Int32Array | Uint8Array;

export function isTypedArray(a: {}): a is Int32Array | Uint8Array {
  return a instanceof Int32Array || a instanceof Uint8Array;
}

export function flatten<T extends number|TypedArray>(arr: T) {
  if (isTypedArray(arr)) {
      arr[1]; // TS 7053
  }
}

3.8 behavior: No error
3.9 behavior: Error

Playground Link: https://www.typescriptlang.org/play/?ts=3.9.0-dev.20200420&ssl=1&ssc=1&pln=11&pc=2#code/KYDwDg9gTgLgBDAnmYcAqzgBMCCUoCGicAvHAJIB2MAzAEx6HEA+cAqgJbUAcjRA3AChBoSLDgAzAK6UAxjA4RKcDgGcMKXPiIAKAgC44AbwC+ASkMEVqitXp8W7LjF7biRwXDhRgMKVGUrLlUYAjlgCAlbWgY3OGZWIMoQsNkIqM4eByETYVFoeGk5BSVJABsCGBhgSgAeNDhQasosG0opAFsAI2AoZg1sBwA+PXxDNDNjTxUonTUBrSZRqDNJjy8NuAJ8AG0ARgBdIS9ckyA

Related Issues: I'm guessing this is another empty intersection bug, but I wasn't sure how to de-dup it.

Inspiration is from https://github.com/tensorflow/tfjs/blob/95995e7fec478e86b4baf0d34f902d3e345ee54b/tfjs-core/src/util.ts#L144

@amcasey
Copy link
Member Author

amcasey commented Apr 22, 2020

FYI @ahejlsberg

@amcasey
Copy link
Member Author

amcasey commented Apr 22, 2020

The error text is

Element implicitly has an 'any' type because expression of type '1' can't be used to index type '(T & Int32Array) | (T & Uint8Array)'.
  Property '1' does not exist on type '(T & Int32Array) | (T & Uint8Array)'.(7053)

@amcasey amcasey changed the title New error in 3.9: TS 7503 - can't index with number New error in 3.9: TS 7053 - can't index with number Apr 22, 2020
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 22, 2020
@RyanCavanaugh
Copy link
Member

@ahejlsberg not sure why this changed; I don't recall any changes around indexing. Observations:

  • The narrowed type of arr is the same in 3.9 and 3.8.3
  • arr[1] has type number in 3.8.3
  • Changing to arr[1 as number] doesn't fix it (just produces an error about no numeric index signature instead)

@ahejlsberg
Copy link
Member

It appears to have something to do with discriminants and intersections. Simplified example:

interface NumList {
  kind: 'n';
  [x: number]: number;
}
interface StrList {
  kind: 's';
  [x: number]: string;
}

export function foo<T extends NumList | StrList>(arr: T & NumList | T & StrList) {
  let zz = arr[1];  // Error
}

Error goes away without the kind property or when it is changed to something that isn't a discriminant.

@DanielRosenwasser
Copy link
Member

Didn't @orta look into a similar bug?

@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Apr 30, 2020
@ahejlsberg ahejlsberg added this to the TypeScript 3.9.2 milestone Apr 30, 2020
@ahejlsberg ahejlsberg added the Fix Available A PR has been opened for this issue label Apr 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants