-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Intersection with type parameter doesn't subtract properties #20536
Comments
Looks like a duplicate of #20505. |
I'm not sure, but I don't think it's a duplicate. interface InternalProps {
a: number;
}
interface ExternalProps {
b: string;
}
function make<T = {}>(arg: T & InternalProps): T & ExternalProps {
return 1 as any as T & ExternalProps;
}
const x = make<{}>({a: 1});
console.log(x.a); // Correct: type error
console.log(x.blah); // Correct: type error
const y = make({a: 1});
console.log(y.a); // Incorrect: no type error
console.log(y.blah); // Correct: type error
|
The thing they have in common is that adding a default to a function's type parameter doesn't turn off type inference. So if a type can be inferred, that type will be used instead of the default. But in this case I see that declare function removeA<T>(arg: T & { a: any }): T;
const o = removeA({a: 1});
o.a; // Should be an error? |
@andy-ms, yes if the example you show was implemented, |
The compiler can not make these assumptions that the property exclusively exists in one constituent of the intersection.. i.e. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.6.1 && 2.7.0-dev.20171207
Code
Expected behavior:
Property
a
should not be expected to exist ony
.Both examples should fail.
Actual behavior:
Only first example is considered a type error.
The text was updated successfully, but these errors were encountered: