Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union of classes with same structure collapsed, leading to missing type errors #61482

Closed
blickly opened this issue Mar 25, 2025 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@blickly
Copy link
Contributor

blickly commented Mar 25, 2025

πŸ”Ž Search Terms

collapsed union, instanceof, structural types, nominal types

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about nominal types and instanceof

⏯ Playground Link

https://www.typescriptlang.org/play/?downlevelIteration=true&importHelpers=true&target=99&module=1&ts=5.8.2#code/MYGwhgzhAEBiD28A8AVAfNA3gKGtY8AdhAC4BOArsCfGQBRkCmYAJkSAJ7QAeAXNCgCUWAL65oAdwCWJABYANVGjpgyAc35D+CZOizi8TEhTKFohRhLiIV6wQG5xYsdlCQYAIVVL9eAsXIqGnomVnYuPgFhTDE8aTlFdFsNKP4vMh8cPENGY1NzS2h05IcnbBcAMwpCaikiaArEAHkydKVkzUFtRCUAHza9LOgjEzMLK2LVNVLK6tr690YyEgBlcilCNTpI0jIN6d9oKQroOhIOAAdGeBPuaABCAF5H6AByXf3X4TkyeCtx6AAUTIv3or0YjAA1vcvo5ZjUSHUzBU6F1oAA3eBSFiHfykHjQF6NeAtYoAFgATKU8AB6GnZaAAPQA-OI8SRoFwXtwAHTxBR0ACM1OgdIZLPEx1OXA2pDANWuJx00QMw1yo0ceFi0EWyzWe02dA4DlF9JlMAs6KWABpoBB4NA5FIYOcrvhZIxgJCYE0ANLiMUMoNM1kiIA

πŸ’» Code

class Foo<T> {
  constructor(readonly x: T) {}
  withX<T>(arg: T): Foo<T> {
    return new Foo(arg);
  }
}

class Bar<T> {
  constructor(readonly x: T) {}
  withX<T>(arg: T): Bar<T> {
    return new Bar(arg);
  }
}

function fooOrBar<T>(arg: T): Foo<T>|Bar<T> {
  return new Bar(arg);
}

function assertString(x: string) {
  if (typeof x !== 'string') throw new Error('eek!');
}

function f(): void {
  const x = fooOrBar(42);
  const y = x.withX(1);
  if (y instanceof Foo) {
    return;
  }
  assertString(y); // y is never, so this type checks OK
}

πŸ™ Actual behavior

assertString call gives no error

πŸ™‚ Expected behavior

assertString gives an error, since neither Foo nor Bar are assignable to string

Additional information about the issue

This appears somewhat related to #202 (and having a way to force classes to be treated nominally would have avoided this issue for us). However, this issue does seem distinct in that it has a missing error even when classes are interpreted structurally.

@MartinJohns
Copy link
Contributor

Duplicate of #60033.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 27, 2025
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants