-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Accessing property in union of object types fails for properties not defined on all union members #12815
Comments
The doc says : To get the same code working, we’ll need to use a type assertion: let pet = getSmallPet();
if ((<Fish>pet).swim) {
(<Fish>pet).swim();
}
else {
(<Bird>pet).fly();
} http://www.typescriptlang.org/docs/handbook/advanced-types.html Then in your sample: if ((<A>AorB).a) {
// use AorB.a
} |
The issue here is that because type A = { a: string; }
type B = { b: string; a: undefined }
type AorB = A | B;
declare const AorB: AorB;
if (AorB.a) {
// Ok
} |
Makes perfect sense. Brain fart. |
If you define |
If I understand the comments correctly, this should work (but throws; tested on playground): Is this a bug or not? 🤔 type LinkProps = {
to: string;
onClick?: undefined;
// Link specific props:
target: string;
}
type ButtonProps = {
to?: undefined;
onClick: Function;
// Button specific props:
disabled: boolean;
}
type ActionProps = LinkProps | ButtonProps;
const Action = (props: ActionProps) =>
props.to ?
'Link with target: ' + props.target // Error not on ButtonProps
:
'Button with disabled: ' + props.disabled; // Error: not on LinkProps
Action({
to: 'dssd',
target: '_blank'
}); |
I don't understand this at all. Isn't |
might be better |
What if you don't give a name to each of the members of the union? (can't make a type assertion as above without a name for the asserted type, can I?) type u = "str" | {prop:"val"};
function f(arg:u){return arg.prop} // TypeScript: Property 'prop' does not exist on type 'u' |
Any solution? |
i find a solution on book :
Excerpt From: Basarat Ali Syed. “TypeScript Deep Dive.” Apple Books. a property on an object and can be used as a type guard, and the TypeScript can know which type you used. |
Does not work for me 07.10.2020 And I think that this behavior of TS is not good because it does not help a developer much... |
|
@thorn0 yes, because if you write code like if (a.foo) { you might be thinking you're testing for the non-zeroness of |
@RyanCavanaugh Thanks for the answer, but I'm having trouble understanding it. Could you put it another way please? upd: Turns out there is an open issue for this: #1260. But I still don't understand your point about ambiguity. |
TypeScript Version: 2.1.1
Code
Expected behavior:
The code compiles without errors. Even though
a
doesn't (necessarily) exist on all the constituents of the union, the fact that it exists on some should allow me to check for.a
and use it if it is present.Actual behavior:
Typescript complains: "Property a does not exist on type AorB... Property a does not exist on type B."
The text was updated successfully, but these errors were encountered: