Thanks to visit codestin.com
Credit goes to github.com

Skip to content

assertion does not handle unioin type correctlyΒ #62454

@GuichiZhao

Description

@GuichiZhao

πŸ”Ž Search Terms

assetion union type

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.3#code/C4TwDgpgBAysBOBLAdgcwPLwHIFcC2ARhPFALxQDOCKqUAPlMvkfANwBQoks1amAQgHtBAGwgBDZGUq9aDAsLGSO7ACYQAxiPHxoY4FABu4kTggBGAFw8kfbM2JQO6rTr0QDx0xABM1uLYY8EKiElIqAPQRRiZm5tJeZj4c+lAagsiGxMAQqgmxFlDiFDY0AophrFBAA

πŸ’» Code

type StringOrNumber = string | number;
type StringOrBoolean = string | boolean;

declare let value1: StringOrNumber ;
declare let value2: StringOrBoolean ;

// value1 = value2;
let converted = value1 as StringOrBoolean;

StringOrNumber and StringOrBoolean do not have a subtype-supertype relationship, i.e., they can not be assigned to each other; how can they be asserted to each other then?

The LLM said this is legal because they overlap somehow, however

type A = { a: string; b: number };
type B = { a: string; c: boolean };

let a: A = { a: "test", b: 123 };
let b = a as B;

Such code is treated as legal by LLM, but it does not actually which is expected

How come value1 as StringOrBoolean is legal, is this a bug?

πŸ™ Actual behavior

type StringOrNumber = string | number;
type StringOrBoolean = string | boolean;

declare let value1: StringOrNumber ;
declare let value2: StringOrBoolean ;

// value1 = value2;
let converted = value1 as StringOrBoolean;

Do not throw an error

πŸ™‚ Expected behavior

It should throw an error

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions