TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Crash when defining `Array` type via `[]` syntax

Open rotu opened this issue 1 year ago • 5 comments

🔎 Search Terms

Array type define crash

🕗 Version & Regression Information

  • This is a crash

⏯ Playground Link

https://www.typescriptlang.org/dev/bug-workbench/?noLib=true#code/PTAEAEDsHsBkEsBGAuUAXATgVwKYCg0BPABx1AEEMMBDQgHgBUA+UAXlAYG0BdAbjyA

💻 Code

// @noLib: true
type Array<T> = T[];

🙁 Actual behavior

Typescript crashes:

createDeferredTypeReference@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:52280:42
getTypeFromArrayOrTupleTypeNode@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:52908:93
getTypeFromTypeNodeWorker@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:54875:20
getTypeFromTypeNode@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:54828:45
getDeclaredTypeOfTypeAlias@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:49869:33
tryGetDeclaredTypeOfSymbol@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:49955:18
getDeclaredTypeOfSymbol@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:49948:16
getTypeOfGlobalSymbol@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:52644:45
getGlobalType@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:52678:42
initializeTypeChecker@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:75719:27
createTypeChecker@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:42674:7
getTypeChecker@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:101959:46
synchronizeHostDataWorker@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:121935:17
synchronizeHostData@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:121830:11
getProgram@https://typescript.azureedge.net/cdn/5.4.0-dev.20240110/monaco/dev/vs/language/typescript/tsWorker.js:121997:9
updateFile@https://www.typescriptlang.org/17821066377c4486a980330106160c805ea5b4a9-5466acbcf54af90f679c.js:1:25348
G@https://www.typescriptlang.org/17821066377c4486a980330106160c805ea5b4a9-5466acbcf54af90f679c.js:1:17599
F@https://www.typescriptlang.org/17821066377c4486a980330106160c805ea5b4a9-5466acbcf54af90f679c.js:1:17638
A@https://www.typescriptlang.org/component---src-pages-dev-bug-workbench-tsx-6a9bdea3e00be7d97087.js:1:40663
9796/M/

🙂 Expected behavior

I expect an error message.

Additional information about the issue

No response

rotu avatar Jan 10 '24 22:01 rotu

Just to be clear, this isn't going to work

RyanCavanaugh avatar Jan 11 '24 00:01 RyanCavanaugh

Just to be clear, this isn't going to work

lol, I know. I reduced the repro example from something like type Array<T> = T[] & { push: (t: T) => number } (which is also wrong)

I expect this code to give an error about a circular type reference as if I had written type Array<T> = Array<T>

rotu avatar Jan 11 '24 02:01 rotu

It seems there's an internal failure that happens before this crash. It's not my particular definition of the Array type that's a problem but using a type alias to define Array at all:

// @noLib: true

// okay with this definition of Array:
// interface Array<T> {
//   [i:number]:T
// }

// okay with this definition of Array, too:
// class Array<T> {
//   [i:number]:T
// }

// With this definition, things fail and number[] is not the same type as Array<T> anymore:
type Array<T> = {
  [i:number]:T
}

[1,2,3] satisfies {[i:number]:any}

declare const x:number[]
//            ^?
x satisfies Array<number>

Workbench Repro

rotu avatar Jan 11 '24 04:01 rotu

:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


Comment by @rotu

:x: Failed: -

  • Type '{}' does not satisfy the expected type '{ [i: number]: any; }'. Index signature for type 'number' is missing in type '{}'.
  • Type '{}' does not satisfy the expected type 'Array'. Index signature for type 'number' is missing in type '{}'.
Historical Information
Version Reproduction Outputs
4.9.3, 5.0.2, 5.1.3, 5.2.2, 5.3.2

:x: Failed: -

  • Type '{}' does not satisfy the expected type '{ [i: number]: any; }'. Index signature for type 'number' is missing in type '{}'.
  • Type '{}' does not satisfy the expected type 'Array'. Index signature for type 'number' is missing in type '{}'.

typescript-bot avatar Feb 15 '24 08:02 typescript-bot

It seems there's an internal failure that happens before this crash. It's not my particular definition of the Array type that's a problem but using a type alias to define Array at all

moved this to standalone issue #57564

rotu avatar Feb 27 '24 19:02 rotu