TypeScript Crash when defining Array type via [] syntax

gk7wooem  于 10个月前  发布在  TypeScript
关注(0)|答案(5)|浏览(86)

🔎 搜索词

数组类型定义崩溃

🕗 版本与回归信息

  • 这是一次崩溃

⏯ Playground链接

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

💻 代码

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

🙁 实际行为

Typescript崩溃:

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

🙂 预期行为

我期望看到一个错误信息。

关于问题的附加信息

  • 无响应*
qybjjes1

qybjjes11#

为了澄清,这不会起作用。

xmjla07d

xmjla07d2#

为了澄清,这不会起作用。我知道。我将重现示例从类似于type Array<T> = T[] & { push: (t: T) => number }(也是错误的)的东西减少了。我希望这段代码会给出一个关于循环类型引用的错误,就像我写的type Array<T> = Array<T>一样。

fcg9iug3

fcg9iug33#

似乎在这次崩溃之前发生了内部故障。这不是我对 Array 类型的特定定义有问题,而是使用类型别名来定义 Array 本身:

  1. // @noLib: true
  2. // okay with this definition of Array:
  3. // interface Array<T> {
  4. // [i:number]:T
  5. // }
  6. // okay with this definition of Array, too:
  7. // class Array<T> {
  8. // [i:number]:T
  9. // }
  10. // With this definition, things fail and number[] is not the same type as Array<T> anymore:
  11. type Array<T> = {
  12. [i:number]:T
  13. }
  14. [1,2,3] satisfies {[i:number]:any}
  15. declare const x:number[]
  16. // ^?
  17. x satisfies Array<number>

Workbench Repro

展开查看全部
lb3vh1jj

lb3vh1jj4#

你好,我是 Repro bot 。我可以协助缩小范围并跟踪编译器错误!此评论反映了当前运行在夜间 TypeScript 的重现情况。

@rotu 的评论
❌ 失败:-

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

历史信息
| 版本 | 重现输出 | 时间 |
| ------------ | ------------ | ------------ |
| 5.0.2 | ❌ 失败:-* 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 '{}'. | |
| 4.9.3, 5.1.3, 5.2.2, 5.3.2 | ❌ 失败:-* 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 '{}'. | ⚠️ 更慢 |

展开查看全部
yvgpqqbh

yvgpqqbh5#

看起来在这次崩溃之前发生了内部故障。这不是我对 Array 类型的特定定义有问题,而是使用类型别名来定义 Array 本身
已将此问题移动到独立问题 #57564

相关问题