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

A signature using union type does not produce the same invalid usages than a signature using union types #46873

Closed
stof opened this issue Nov 19, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@stof
Copy link

stof commented Nov 19, 2021

Bug Report

The handbook recomments using overloads rather than a union when only the argument type changes. But this does not produce the same behavior regarding catching extra object literal properties.

🔎 Search Terms

overload, argument, union

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type system

⏯ Playground Link

Playground link with relevant code

💻 Code

interface RgbColor {
  red: number;
  green: number;
  blue: number;
  alpha?: number;
}

interface HslColor {
  hue: number;
  saturation: number;
  lightness: number;
  alpha?: number;
}

interface HwbColor {
  hue: number;
  whiteness: number;
  blackness: number;
  alpha?: number;
}
export class SassColorOverload {
  constructor(color: RgbColor);
  constructor(color: HslColor);
  constructor(color: HwbColor);
  constructor(color: RgbColor | HslColor | HwbColor) {
    // TODO
  }
}
export class SassColorUnion {
  constructor(color: RgbColor | HslColor | HwbColor) {
    // TODO
  }
}
new SassColorOverload({
  red: 255,
  green: 255,
  blue: 255,
  hue: 0,
  saturation: 0,
  lightness: 100,
})
new SassColorUnion({
  red: 255,
  green: 255,
  blue: 255,
  hue: 0,
  saturation: 0,
  lightness: 100,
})

🙁 Actual behavior

SassColorOverload reports an invalid object literal, but SassColorUnion does not.

🙂 Expected behavior

Same behavior regarding catching extra properties in object literals when using the union type, especially given that this is the recommended implementation in https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#use-union-types

@MartinJohns
Copy link
Contributor

Duplicate of #20863.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 22, 2021
@typescript-bot
Copy link
Collaborator

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

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