ITADN

TS2742 error when using `Conditional` with generics

#1545ClosedTastyPi 创建于 2026-03-13
T
TastyPicommented
I annoyingly have not managed to reproduce this in the TypeScript Playground, but the following code: ```typescript import { Type } from "typebox"; export function Test<Kind extends number>(solution: Kind) { return Type.Conditional( Type.Literal(solution), Type.Literal(1), Type.String(), Type.Number(), ); } ``` with the following `tsconfig` options: ```json { "extends": ["@tsconfig/node24/tsconfig.json"], "compilerOptions": { "allowImportingTsExtensions": false, "allowJs": true, "composite": true, "declaration": true, "declarationMap": true, "esModuleInterop": true, "incremental": true, "isolatedModules": true, "module": "NodeNext", "moduleResolution": "nodenext", "noEmitOnError": true, "noFallthroughCasesInSwitch": true, "noImplicitOverride": true, "noImplicitReturns": true, "noUnusedLocals": true, "sourceMap": true, "strict": true, "strictNullChecks": true } } ``` is giving me these errors: ``` test.ts:3:17 - error TS2742: The inferred type of 'Test' cannot be named without a reference to 'node_modules/typebox/build/type/extends/extends-left.mjs'. This is likely not portable. A type annotation is necessary. 3 export function Test<Kind extends number>(solution: Kind) { ~~~~ test.ts:3:17 - error TS2742: The inferred type of 'Test' cannot be named without a reference to 'node_modules/typebox/build/type/extends/literal.mjs'. This is likely not portable. A type annotation is necessary. 3 export function Test<Kind extends number>(solution: Kind) { ~~~~ test.ts:3:17 - error TS2742: The inferred type of 'Test' cannot be named without a reference to 'node_modules/typebox/build/type/script/parser.mjs'. This is likely not portable. A type annotation is necessary. 3 export function Test<Kind extends number>(solution: Kind) { ~~~~ test.ts:3:17 - error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. 3 export function Test<Kind extends number>(solution: Kind) { ~~~~ ``` This looks similar to this issue https://github.com/sinclairzx81/typebox/issues/722#issuecomment-1884159833, where the solution was to export more types. Could that be done here too please?
关闭于 2026-03-15 2 条评论