microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.17k stars 12.51k forks source link

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

Open rotu opened 10 months ago

rotu commented 10 months ago

πŸ”Ž Search Terms

Array type define crash

πŸ•— Version & Regression Information

⏯ 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

RyanCavanaugh commented 10 months ago

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

rotu commented 10 months ago

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 commented 10 months ago

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

typescript-bot commented 9 months ago

: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: -

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 '{}'.

rotu commented 8 months ago

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