Skip to content

Regression from TypeScript v5.0: change in type narrowing behavior #19

@MajorLift

Description

@MajorLift

A regression is introduced by upgrading TypeScript to v5.0. This is caused by a breaking change in TypeScript's type narrowing behavior.

  • Example:
type Type = number | Date | ...
let value: Type

if (typeof value === 'number' || value instanceof Date) {
  // Type of 'value' is not `number | Date`, but `(Type & number) | (Type & Date)`.
}
  • Error:
TypeScriptError: Failed to initialise the project.
src/structs/refinements.ts:165:10 - error TS2365: Operator '<=' cannot be applied to types 'number' and '(Type & number) | (Type & Date)'.

165         (minimum <= value && value <= maximum) ||
             ~~~~~~~~~~~~~~~~
src/structs/refinements.ts:165:30 - error TS2365: Operator '<=' cannot be applied to types '(Type & number) | (Type & Date)' and 'number'.

165         (minimum <= value && value <= maximum) ||
                                 ~~~~~~~~~~~~~~~~

    at getProgram (file:///Volumes/Code/metamask/superstruct/node_modules/@ts-bridge/cli/dist/build.js:36:15)
    at buildHandler (file:///Volumes/Code/metamask/superstruct/node_modules/@ts-bridge/cli/dist/build.js:69:21)
    at Object.handler (file:///Volumes/Code/metamask/superstruct/node_modules/@ts-bridge/cli/dist/cli.js:53:16)
    at /Volumes/Code/metamask/superstruct/node_modules/yargs/build/index.cjs:1:8993
    at j (/Volumes/Code/metamask/superstruct/node_modules/yargs/build/index.cjs:1:4956)
    at _.handleValidationAndGetResult (/Volumes/Code/metamask/superstruct/node_modules/yargs/build/index.cjs:1:8962)
    at _.applyMiddlewareAndGetResult (/Volumes/Code/metamask/superstruct/node_modules/yargs/build/index.cjs:1:9604)
    at _.runCommand (/Volumes/Code/metamask/superstruct/node_modules/yargs/build/index.cjs:1:7231)
    at [runYargsParserAndExecuteCommands] (/Volumes/Code/metamask/superstruct/node_modules/yargs/build/index.cjs:1:58912)
    at te.parse (/Volumes/Code/metamask/superstruct/node_modules/yargs/build/index.cjs:1:40478)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions