You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interface IFoo {
name: string;
}
interface IBar {
aNumber: number;
anArray: string[];
}
function test<T extends IFoo>() {
const obj: T & IBar & { anotherProp: boolean } = {
name: "Testing", //Errors here (TS 3.2.1). Not in TS 3.1.4
aNumber: 1,
anArray: ["a", "b", "c"],
anotherProp: true
};
}
function test2<T extends IFoo = IFoo>() {
const obj: T & IBar & { anotherProp: boolean } = {
name: "Testing", //Errors here (TS 3.2.1). Not in TS 3.1.4
aNumber: 1,
anArray: ["a", "b", "c"],
anotherProp: true
};
}
Expected behavior:
name being a property of the generic placeholder T (because it's constrained to something that must be or extend from IFoo) should be a valid property of the above intersection type.
Actual behavior:
TS compiler error on the name property (Object literal may only specify known properties and 'name' does not exist ... forgot the actual TS error code here).
This did not happen in TS 3.1.4 (which I had upgraded from) and possibly older versions too
Playground Link: (https://bit.ly/2rfqphb) Had to use bit.ly to shorten the playground link because playground because the < and > characters mess up markdown link formatting here.
Related Issues:
The text was updated successfully, but these errors were encountered:
When the target of an assignment is an intersection that includes higher order types (such as type parameters) and the source has no higher order types, the assignment will always be an error. So, we definitely shouldn't be doing excess property checking in that case.
So, what I'm saying is that (a) your example is definitely an error, but (b) the way we report the error is confusing. To understand why the example is an error, consider this simple case:
This is an error because T may refer to a type that has additional properties beyond name, and those properties are obviously missing in the assignment.
TypeScript Version: 3.2.1
Search Terms: intersection / intersection placeholder
Code
Expected behavior:
name
being a property of the generic placeholderT
(because it's constrained to something that must be or extend fromIFoo
) should be a valid property of the above intersection type.Actual behavior:
TS compiler error on the
name
property (Object literal may only specify known properties and 'name' does not exist ... forgot the actual TS error code here).This did not happen in TS 3.1.4 (which I had upgraded from) and possibly older versions too
Playground Link: (https://bit.ly/2rfqphb) Had to use bit.ly to shorten the playground link because playground because the
<
and>
characters mess up markdown link formatting here.Related Issues:
The text was updated successfully, but these errors were encountered: