Skip to content
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

Closed
mizunashi-mana opened this issue Sep 9, 2015 · 8 comments
Closed

How can I guard interface argument type of function type? #4704

mizunashi-mana opened this issue Sep 9, 2015 · 8 comments

Comments

@mizunashi-mana
Copy link

// test.ts

interface A {
  a: string;
}
interface B extends A {
  b: string;
}
const a = (a: B) => a.b;
const b = (a: (a: A) => string) => a({a: "param a"});
console.log(b(a));

This is valid typescript code.
But, of course:

$ tsc test.ts
$ node test.js
undefined
$

Is this bug?
Or my misunderstanding?

@mizunashi-mana
Copy link
Author

As a reference, this sample passed Online Check (http://goo.gl/8020p7)

@kimamula
Copy link
Contributor

kimamula commented Sep 9, 2015

Your code looks totally valid and should output undefined with no wonder.
Which part looks a bug to you?

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));

@mizunashi-mana
Copy link
Author

Sorry & thx for fixing my sample code.

I wondered func2(func1) is valid typescript code.

In the case:

const b: B = { a: "param a", b: "param b" };
const a: A = b;

This is valid because interface B must have all of interface A's. It is safe.

However, in the case using function, I think non safe.
This is clear for the sample code.

interface B can behave like interface A, but, there are cases (b: B) => any cannot behave like (a: A) => any.

@mizunashi-mana
Copy link
Author

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 undefined, but, this has trouble.

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.
I have less knowledge. So, if you know the reason, please tell me.

@kimamula
Copy link
Contributor

kimamula commented Sep 9, 2015

Ah, I see, you are talking about something related to PECS (Producer Extends and Consumer Super).
In TypeScript, if B extends A, then SomethingGeneric<B> extends SomethingGeneric<A> and this can be problematic in some cases as you mentioned.

I don't know if there is a explicit reason for this behavior (probably for simplicity, I suppose).
Changing this behavior would be a big breaking change and I'm not sure if everyone is willing to accept it (for me, it's not bad).

@falsandtru
Copy link
Contributor

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

@mhegazy
Copy link
Contributor

mhegazy commented Sep 9, 2015

looks like a dupe of #1394

@mizunashi-mana
Copy link
Author

@mhegazy oh, thx.

Let me discuss in that issue.

Thx for all of advicers.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants