microsoft / TypeScript

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

5.6.3 tsc crash: Error: Debug Failure. False expression. at getOptionalType #60390

Open sebcode opened 3 weeks ago

sebcode commented 3 weeks ago

🔎 Search Terms

Error: Debug Failure. False expression. at getOptionalType

🕗 Version & Regression Information

Crashes with 5.6.3 and next, but works fine with previous release 5.5.4.

⏯ Playground Link

Couldn't get it to work in the playground. Instead, see below for a repro.

💻 Code

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

const slice = createApi({
    reducerPath: "userApi",
    baseQuery: fetchBaseQuery({}),
    endpoints: (builder) => ({
        test: builder.query<any, void>({
            query: () => ({
                url: `/user`,
            }),
        }),
    }),
});

export const { useTestQuery } = slice;

🙁 Actual behavior

Crash

Error: Debug Failure. False expression.
    at getOptionalType (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:66507:11)
    at Object.canReuseTypeNodeAnnotation (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:50282:28)
    at serializeTypeAnnotationOfDeclaration (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:131464:19)
    at typeFromProperty (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:131687:36)
    at Object.serializeTypeOfDeclaration (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:131519:16)
    at serializeTypeForDeclaration (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:52429:41)
    at addPropertyToElementList (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:51321:43)
    at createTypeNodesFromResolvedType (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:51217:11)
    at createTypeNodeFromObjectType (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:50984:25)
    at visitAndTransformType (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:50907:24)

🙂 Expected behavior

No crash.

Additional information about the issue

Repro:

$ git clone https://github.com/sebcode/tsccrashbugrepro.git
$ cd tsccrashbugrepro/
$ yarn
$ yarn tsc
yarn run v1.22.21
warning package.json: No license field
$ /home/seb/tmp/1/node_modules/.bin/tsc
/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:121666
      throw e;
      ^

Error: Debug Failure. False expression.
    at getOptionalType (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:66507:11)
    at Object.canReuseTypeNodeAnnotation (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:50282:28)
    at serializeTypeAnnotationOfDeclaration (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:131464:19)
    at typeFromProperty (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:131687:36)
    at Object.serializeTypeOfDeclaration (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:131519:16)
    at serializeTypeForDeclaration (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:52429:41)
    at addPropertyToElementList (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:51321:43)
    at createTypeNodesFromResolvedType (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:51217:11)
    at createTypeNodeFromObjectType (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:50984:25)
    at visitAndTransformType (/home/seb/tmp/1/node_modules/typescript/lib/_tsc.js:50907:24)

Node.js v20.8.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Andarist commented 3 weeks ago

Self-isolated repro (workbench):

// @strictNullChecks: false
// @declaration: true
// @emitDeclarationOnly: true

// @filename: createApi.ts

type Id<T> = {
  [K in keyof T]: T[K];
} & {};

export declare function createApi<Definitions>(_: { endpoints: Definitions }): {
  [K in keyof Definitions as `use${Capitalize<K & string>}Query`]: () => Id<{
    status: "uninitialized";
    originalArgs?: undefined;
  }>;
};

// @filename: index.ts

import { createApi } from "./createApi";

const slice = createApi({
  endpoints: {
    test: {
      url: `/user`,
    },
  },
});

export const { useTestQuery } = slice;