-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Inferred type depends on physical order of method overloads in code #24275
Comments
|
Is there any way to workaround this limitation? Haven't seen this information about function overloading in other places and there is no any tsc Warning/Error so I'm not sure everybody using TypeScript considers this approach as a best practice. I see that it seems to be by design for interface OriginalApi1 {
fn(p: string): string;
fn(p: number): string;
}
// Same as OriginalApi1 but with diffrent order of methods
interface OriginalApi2 {
fn(p: number): string;
fn(p: string): string;
}
// Creates wrapper type which has methods accepting only string parameters
type Wrapper<T> = { [K in keyof T]: T[K] extends (p: infer P1) => any ? P1 extends string ? (p: P1) => P1 : never : never };
let a: Wrapper<OriginalApi1>;
let b: Wrapper<OriginalApi2>;
a.fn(); // Error, (property) fn: never
b.fn('str'); // OK, (property) fn: (p: string) => string I was trying to do something similar without interface OriginalApi1 {
fn1(p: string): string;
fn1(p: number, p2: number): string;
fn2(p: number, p2: number): string;
}
// Filters methods by parameter type
type Filter<T> = { [K in keyof T]: T[K] extends (p: string) => any ? T[K] : never }
let c: Filter<OriginalApi1>;
c.fn1('str'); // OK
c.fn1(23, 42); // OK, why? error expected
c.fn2(23, 42); // Error, (property) fn2: never, as expected |
Overload resolution is currently a zero-order process, and is not deferred nor does it work on higher order types. see related discussion in #6606 (comment) |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
…on and adds deleteUserRole utility (#90533) ## Summary Unskips tests after a ES promotion and adds a delete user role utility. Ref: #90229 #88302 Removes one `any` from the utils by switching to using `ProvidedType` Before: <img width="558" alt="Screen Shot 2021-02-05 at 2 45 37 PM" src="https://user-images.githubusercontent.com/1151048/107098890-8dce5b80-67cd-11eb-8f6e-51f83eef4647.png"> After: <img width="513" alt="Screen Shot 2021-02-05 at 4 13 23 PM" src="https://user-images.githubusercontent.com/1151048/107098898-9161e280-67cd-11eb-8085-a5220938834e.png"> Turns out that return types on overloaded functions aren't easy fwiw and will fall on the bottom one which in this case looked to be `any` which we don't want: microsoft/TypeScript#24275 (comment) ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
…romotion and adds deleteUserRole utility (#90533) (#90847) * [Security Solutions][Detection Engine] Unskips tests after ES promotion and adds deleteUserRole utility (#90533) ## Summary Unskips tests after a ES promotion and adds a delete user role utility. Ref: #90229 #88302 Removes one `any` from the utils by switching to using `ProvidedType` Before: <img width="558" alt="Screen Shot 2021-02-05 at 2 45 37 PM" src="https://user-images.githubusercontent.com/1151048/107098890-8dce5b80-67cd-11eb-8f6e-51f83eef4647.png"> After: <img width="513" alt="Screen Shot 2021-02-05 at 4 13 23 PM" src="https://user-images.githubusercontent.com/1151048/107098898-9161e280-67cd-11eb-8085-a5220938834e.png"> Turns out that return types on overloaded functions aren't easy fwiw and will fall on the bottom one which in this case looked to be `any` which we don't want: microsoft/TypeScript#24275 (comment) ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios # Conflicts: # x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_index.ts # x-pack/test/detection_engine_api_integration/security_and_spaces/tests/delete_signals_migrations.ts * Fixes test failure as the permissions are true for 7.x -> 7.12 compared to false for 7.11
TypeScript Version: 2.9.0-dev.20180519
Search Terms:
infer, extract function parameter types, function wrapper
Code
gist https://gist.github.com/Le0Michine/395417fbf050ec8d8aca0721e32b575a
interfaces created specifically for this example, in my use case there is actually class declaration in a
*.d.ts
file, this is the reason why it doesn't look like this:Expected behavior:
resulting type for
Wrapper<OriginalApi1>
andWrapper<OriginalApi2>
should be the same, asOriginalApi1
andOriginalApi2
are identical typesActual behavior:
types
Wrapper<OriginalApi1>
andWrapper<OriginalApi2>
are different despite the fact thatOriginalApi1
andOriginalApi2
are the samePlayground Link:
https://www.typescriptlang.org/play/#src=...
Related Issues:
no
The text was updated successfully, but these errors were encountered: