microsoft / microsoft/TypeScript

API: Exception in TypeChecker.typeToTypeNode

オープン
#45,515 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

@rbuckton がすでに取り組んでいます。

2021年8月19日 から。

Needs Investigation Rescheduled
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

typeToTypeNode kind undefined

🕗 Version & Regression Information

4.3.2 and earlier versions (not sure when it started)

  • This is a crash
⏯ Playground Link

Playground

💻 Code

Using compiler API:

function transformVariableDeclaration(statement: ts.VariableDeclaration, checker: ts.TypeChecker) {
    if (statement.initializer) {
        const initializerType = checker.getTypeAtLocation(statement.initializer);
        const initializerTypeNode = checker.typeToTypeNode(initializerType, undefined, undefined); // TypeError: Cannot read property 'kind' of undefined
        // ...
    }
}

Target code being transpiled:

type Foo = (fn: (...args: [][]) => void) => void;
const foo: Foo = (bar => {});

Exception thrown:

Exception has occurred: TypeError: Cannot read property 'kind' of undefined
  at Object.parenthesizeElementTypeOfArrayType (node_modules\typescript\lib\typescript.js:20825:28)
    at Object.createArrayTypeNode (node_modules\typescript\lib\typescript.js:22343:53)
    at typeReferenceToTypeNode (node_modules\typescript\lib\typescript.js:49344:52)
    at typeToTypeNodeHelper (node_modules\typescript\lib\typescript.js:49085:95)
    at serializeTypeForDeclaration (node_modules\typescript\lib\typescript.js:50323:30)
    at symbolToParameterDeclaration (node_modules\typescript\lib\typescript.js:49803:41)
    at node_modules\typescript\lib\typescript.js:49725:263
    at Array.map (<anonymous>)
    at signatureToSignatureDeclarationHelper (node_modules\typescript\lib\typescript.js:49725:229)
    at createTypeNodeFromObjectType (node_modules\typescript\lib\typescript.js:49296:49)
🙁 Actual behavior

When calling typeToTypeNode an exception is thrown.

🙂 Expected behavior

No exception should be thrown and undefined should be returned.

Tracing through the ts code, undefined is returned from typeReferenceToTypeNode here (linking to online editor since github can't display the file) and there is a comment referencing this issue. That is assigned to the object attempting to be accessed.

The trigger seems to be the rest parameter type being an array of empty tuples. The real code that triggered the issue used some tricky generics to arrive at that type, but this direct version was able to reproduce the same issue.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。