-
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
How can I guard interface argument type of function type? #4704
Comments
As a reference, this sample passed Online Check (http://goo.gl/8020p7) |
Your code looks totally valid and should output To avoid confusion, it'd be better to use distinct variable names. // test.ts
interface A {
a: string;
}
interface B extends A {
b: string;
}
const func1 = (b: B) => b.b;
const func2 = (func: (a: A) => string) => func({a: "param a"});
console.log(func2(func1)); |
Sorry & thx for fixing my sample code. I wondered In the case: const b: B = { a: "param a", b: "param b" };
const a: A = b; This is valid because However, in the case using function, I think non safe.
|
For another example, this is valid case. // test.ts
interface A {
a: () => string;
}
interface B extends A {
b: () => string;
}
const func1 = (b: B) => b.b;
const func2 = (func: (a: A) => (() => string)) => func({a: () => "param a"});
console.log(func2(func1)()); The last sample only printed However, I maybe misunderstand. This is also valid code: // test.ts
interface A {
a: string;
}
interface B extends A {
b: string;
}
interface C<T extends (a: A) => string> {
c: T;
}
var func: C<(a: B) => string>; Perhaps, this maybe be valid for some critical reason. |
Ah, I see, you are talking about something related to PECS (Producer Extends and Consumer Super). I don't know if there is a explicit reason for this behavior (probably for simplicity, I suppose). |
refine // test.ts
interface A {
x: string;
}
interface B {
x: string;
y: string;
}
const a: A = {x: 'x'};
const b: B = a; // <= cannot assign
const fb = (fa: (a: A) => void) => fa({x: 'x'});
const fa= (b: B) => alert(b.y);
fb(fa); // <= can assign but should not be assignable |
looks like a dupe of #1394 |
@mhegazy oh, thx. Let me discuss in that issue. Thx for all of advicers. |
This is valid typescript code.
But, of course:
Is this bug?
Or my misunderstanding?
The text was updated successfully, but these errors were encountered: