Skip to content

Union type not enforcing members correctly #27866

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

Closed
unindented opened this issue Oct 12, 2018 · 1 comment
Closed

Union type not enforcing members correctly #27866

unindented opened this issue Oct 12, 2018 · 1 comment

Comments

@unindented
Copy link

TypeScript Version: 3.1.1

Search Terms: unions

Code

export enum ButtonStatus {
  ENABLED = 'enabled',
  DISABLED = 'disabled',
  LOADING = 'loading',
}

export interface RegularButtonProps {
  readonly status: ButtonStatus;
}

export interface LoadingButtonProps {
  readonly status: ButtonStatus.LOADING;
  readonly loadingText: string;
}

export type ButtonProps = RegularButtonProps | LoadingButtonProps;

const buttonProps: ButtonProps = {
  status: ButtonStatus.LOADING,
  loadingText: 42,
};

Expected behavior:

Error saying loadingText should be of type string.

Actual behavior:

No error. Hovering over loadingText does tell me that loadingText is a string, so the compiler knows but doesn't raise it?

screen shot 2018-10-12 at 11 59 40

Playground Link:

https://www.typescriptlang.org/play/#src=export%20enum%20ButtonStatus%20%7B%0A%20%20ENABLED%20%3D%20'enabled'%2C%0A%20%20DISABLED%20%3D%20'disabled'%2C%0A%20%20LOADING%20%3D%20'loading'%2C%0A%7D%0A%0Aexport%20interface%20RegularButtonProps%20%7B%0A%20%20readonly%20status%3A%20ButtonStatus%3B%0A%7D%0A%0Aexport%20interface%20LoadingButtonProps%20%7B%0A%20%20readonly%20status%3A%20ButtonStatus.LOADING%3B%0A%20%20readonly%20loadingText%3A%20string%3B%0A%7D%0A%0Aexport%20type%20ButtonProps%20%3D%20RegularButtonProps%20%7C%20LoadingButtonProps%3B%0A%0Aconst%20buttonProps%3A%20ButtonProps%20%3D%20%7B%0A%20%20status%3A%20ButtonStatus.LOADING%2C%0A%20%20loadingText%3A%2042%2C%0A%7D%3B%0A

Related Issues:

Mmmaybe this one? #26450

@unindented
Copy link
Author

Ah, sorry, didn't see @DanielRosenwasser's PR: #27087

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant