-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
true
| false
can not be used to create discriminated unions
#17882
Comments
This is working as intended. In the declaration of const optionsOne: One = { kind: true, foo: 'bar' }; Once you have a type annotation the object literal is contextually typed by that annotation and the compiler knows not to widen. For the same reason, there is no error in the call where you directly specify an object literal because the literal is contextually typed by the corresponding parameter type ( |
Thanks for the quick and throughout explanation! My actual use case for this was that I created typings for a library and wanted to have a strongly typed API. This is the original code that caused this issue: // index.d.ts
declare module 'material-ui/List/ListItem' {
import { ButtonBaseProps } from 'material-ui/internal/ButtonBase';
interface ListItemCommonProps extends React.LiHTMLAttributes<HTMLLIElement> {
component?: React.ReactNode;
dense?: boolean;
disabled?: boolean;
disableGutters?: boolean;
divider?: boolean;
}
export type ListItemDefaultProps = {
button?: false;
} & ListItemCommonProps;
export type ListItemButtonProps = {
button?: true;
} & ListItemCommonProps &
ButtonBaseProps;
export type ListItemProps = ListItemDefaultProps | ListItemButtonProps;
export default class ListItem extends MaterialUI.Component<ListItemProps> {}
} And even casting didn't help here: class DockedDrawer extends React.Component<{}, { docked: boolean }> {
state = { docked: true }
render() {
const docked:true|false = this.state.docked;
return (
<Drawer
anchor="bottom"
open={true}
docked={docked}
>
List
</Drawer>
);
} I thought I can use discriminated unions to only allow certain |
For now we don't use them, maybe Reason -> microsoft/TypeScript#17882 helps to find a way to make use of them.
- Update `<Tabs>` API to match BC introduced in #7741 - Update `withStyle` API to match BC introduced in #7740 - Remove discriminated unions (microsoft/TypeScript#17882)
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Hi @sebald maybe if you remove the optional operator from the type, then it would work as you want. You know that the parameter could be either Update: It works as you expect, and narrow down the type of the props. :). export type ListItemDefaultProps = {
button: false;
} & ListItemCommonProps;
export type ListItemButtonProps = {
button: true;
} & ListItemCommonProps; |
TypeScript Version: 2.4.2
Code
Link to the playground
Expected behavior: Should be possible to use
true | false
to create a discriminated union. It seems liketrue | false
andboolean
are not interchangeable.Actual behavior: TypeScript does not widen the above
kind
toboolean
and thusoptionsOne
can not be used as input forfn
. (That's just my wild guess :-x) It's not possible to usetrue
andfalse
for discriminated unions.The text was updated successfully, but these errors were encountered: