Skip to content

Function param with generic type are not recognized correctly #23705

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

Closed
k8w opened this issue Apr 26, 2018 · 9 comments
Closed

Function param with generic type are not recognized correctly #23705

k8w opened this issue Apr 26, 2018 · 9 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@k8w
Copy link

k8w commented Apr 26, 2018

TypeScript Version: 2.7.0-dev.201xxxxx
2.8.3

Search Terms:

Code

class Protocol<T>{}

interface ReqTest {
    abcde: string;
}

const PtlTest = new Protocol<ReqTest>();

function request<T>(ptl: Protocol<T>, req: T) { }

request(PtlTest, {})

Expected behavior:
Compile error.
Because request(PtlTest, {}), param 2 {} not match ReqTest

Actual behavior:
Compile success

Playground Link:

Related Issues:

@ghost
Copy link

ghost commented Apr 26, 2018

You need an explicit type argument here, because {} is a valid inference. PtlTest validly a Protocol<{}> (T is never even used, so everything but null or undefined is aProtocol<T> for any T), and {} of course is a valid {}.

@ghost ghost added the Question An issue which isn't directly actionable in code label Apr 26, 2018
@k8w
Copy link
Author

k8w commented Apr 26, 2018

@Andy-MS shouldn't T be infered from PtlDemo?
Actually it works well in previous TypeScript version.

1 similar comment
@k8w
Copy link
Author

k8w commented Apr 26, 2018

@Andy-MS shouldn't T be infered from PtlDemo?
Actually it works well in previous TypeScript version.

@ghost
Copy link

ghost commented Apr 26, 2018

I don't see a compile error in any of [email protected], [email protected], typescript@next. Protocol is an empty type, so almost anything is assignable to it.

@k8w
Copy link
Author

k8w commented Apr 27, 2018

function test<T>(value1: T, value2: T) {
    return value2
}
test({ a: 1 }, {});

@Andy-MS Just see this.
It would compile error at [email protected]:

error TS2345: Argument of type '{}' is not assignable to parameter of type '{ a: number; }'.
Property 'a' is missing in type '{}'.
But compile succ at [email protected]

And it's strange that it works well if it is this:

function test<T>(value1: T, value2: T) {
    return value2
}
test(<{ a: number }>{ a: 1 }, {});

@k8w
Copy link
Author

k8w commented Apr 27, 2018

class Protocol<T>{ }

interface ReqTest {
    abcde: string;
}

const PtlTest: Protocol<ReqTest> = new Protocol<ReqTest>();

function request<T>(ptl: Protocol<T>, req: T): T { return req; }

request(PtlTest, {})

@Andy-MS And even if I give PtlTest a explicit type Protocol<ReqTest> it still not works.
(and T is used for return type)
{} can not ensure that T are inferenced as the same.

@ghost
Copy link

ghost commented Apr 27, 2018

I'll tentatively mark this as a bug due to the inconsistency in:

declare function equal<T>(a: T, b: T): boolean;
equal("", 0); // Error
equal({ a: 1 }, { b: 2 }); // No error

@ghost ghost added Bug A bug in TypeScript and removed Question An issue which isn't directly actionable in code labels Apr 27, 2018
@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Jul 23, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jul 23, 2018

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants