-
-
Notifications
You must be signed in to change notification settings - Fork 656
Add FunctionOverloads type
#1264
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
61ef2cc
06373ed
3c4541c
c3400fe
04871db
19ea1d7
f140e62
f91847c
33e5b51
24ee5fe
02f0a07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,96 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type {FunctionWithMaybeThisParameter} from './internal/function.d.ts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type {IsEqual} from './is-equal.d.ts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Create a union of all the function's overloads. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
andreww2012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TypeScript's built-in utility types like `Parameters` and `ReturnType` only work with the last overload signature, [by design](https://github.com/microsoft/TypeScript/issues/32164). This type extracts all overload signatures as a union, allowing you to work with each overload individually. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Use-cases: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Extract parameter types from specific overloads using `Extract` and `Parameters` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Analyze all possible function signatures in type-level code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Extract event handler signatures from framework APIs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Known limitions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Functions that have identical parameters but different `this` types or return types will only extract one overload (the last one) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Generic type parameters are lost and inferred as `unknown` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+17
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add at least one example for each case. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```ts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type {FunctionOverloads} from 'type-fest'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function request(url: string): Promise<string>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function request(url: string, options: {json: true}): Promise<unknown>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function request(url: string, options?: {json?: boolean}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type RequestFunctionType = FunctionOverloads<typeof request>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //=> ((url: string) => Promise<string>) | ((url: string, options: {json: true}) => Promise<unknown>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // You can also get all parameters and return types using built-in `Parameters` and `ReturnType` utilities: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type RequestParameters = Parameters<RequestFunctionType>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //=> [url: string, options: {json: true}] | [url: string] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type RequestReturnType = ReturnType<RequestFunctionType>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //=> Promise<string> | Promise<unknown> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This type can also be used to extract event parameter types from framework emit functions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```ts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Given a Vue component that defines its events: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defineEmits<{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| submit: [formData: FormData]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancel: []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Extract the parameter types of the `submit` event: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type {ArrayTail, FunctionOverloads} from 'type-fest'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import HelloWorld from './HelloWorld.vue'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type SubmitEventType = ArrayTail<Parameters<Extract<FunctionOverloads<InstanceType<typeof HelloWorld>['$emit']>, (event: 'submit', ...arguments_: readonly any[]) => void>>>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //=> [formData: FormData] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+57
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Examples should not contain arbitrary code, they should be copy pasteable to the playground.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean the TypeScript playground?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, like the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
andreww2012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @category Function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export type FunctionOverloads<T> = FunctionOverloadsInternal<T>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type FunctionOverloadsInternal< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AllOverloads, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CheckedOverloads = {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MustStopIfParametersAreEqual extends boolean = true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LastParameters = never, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > = AllOverloads extends ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this: infer ThisType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...arguments_: infer ParametersType extends readonly unknown[] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) => infer ReturnType | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? // This simultaneously checks if the last and the current parameters are equal and `MustStopIfParametersAreEqual` flag is true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IsEqual< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [LastParameters, true], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ParametersType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [MustStopIfParametersAreEqual] extends [true] ? true : false, // Prevents distributivity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > extends true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? never | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | FunctionOverloadsInternal< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Normally, in `(FunctionType extends (...args: infer P) => infer R)`, compiler infers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `P` and `R` from the last function overload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // This trick (intersecting one of the function signatures with the full signature) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // makes compiler infer a last overload that do not equal one of the concatenated ones. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Thus, we're ending up iterating over all the overloads from bottom to top. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Credits: https://github.com/microsoft/TypeScript/issues/32164#issuecomment-1146737709 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CheckedOverloads & AllOverloads, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CheckedOverloads & ((this: ThisType, ...arguments_: ParametersType) => ReturnType), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MustStopIfParametersAreEqual extends true ? false : true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ParametersType | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | FunctionWithMaybeThisParameter<ThisType, ParametersType, ReturnType> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : never; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
63
to
95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why only checking the
Suggested change
this will eliminate the current limitation of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When parameters are the same up to With declare const foo: ((this: string) => void) & ((this: number) => string);
foo.call(''); // ts(2345): Argument of type 'string' is not assignable to parameter of type 'number'.With return types being different, it chooses the first overload for some reason: declare const foo: (() => string) & (() => number);
const bar = foo(); // stringAnd in this case, type Test = FunctionOverloads<(() => string) & (() => number)>; // () => numberSo this might need to be fixed, but currently this also matches the built-in type Test = ReturnType<(() => string) & (() => number)>; // number
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand your concern about how overload resolution works in TypeScript’s compile-time enforcement, but it’s important to clarify: the exception behavior you point out applies to overload selection during type checking, not to the type-level utility we’re building. TypeScript will resolve function calls to the first matching overload signature, ignoring differences in return type and sometimes in declare const foo: ((this: string) => void) & ((this: number) => string);
type T = typeof foo
// ^? type T = ((this: string) => void) & ((this: number) => string)
// overloads still preservedOur type utility is meant for static analysis and broader use cases, not just for expressing runtime call behavior. Users might want to use
These use cases go beyond how the runtime implementation behaves. So while matching TypeScript’s call resolution is a good baseline, we shouldn’t constrain the type utility only to what the type checker “chooses” for calls. In short: the overload resolution rules are a valid observation, but they don’t limit what our type utilities can represent. If anything, the distinction shows the need for a richer type-level union so developers can see all overload signatures, even those that TypeScript’s call resolution might ignore during checking. @sindresorhus , @som-sm could you share your thoughts on this?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand your point too, but IMHO matching the actual compiler behavior is more practical and will be sufficient in 95% of situations. If some of the declared overloads get discarded by the compiler, it's likely the sign of the programmer not knowing about this TypeScript peculiarity. If anything, we can introduce a type utility option to control whether these "phantom" overloads will be present in the resulting type or not, but I'm not sure what practical example would I use in this option's docs. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import type {IsUnknown} from '../is-unknown.d.ts'; | ||
|
|
||
| /** | ||
| Constructs a function type with an optional `this` parameter. | ||
andreww2012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| If `this` parameter is not needed, pass `unknown`. | ||
| */ | ||
| export type FunctionWithMaybeThisParameter< | ||
| ThisParameter, | ||
| Parameters_ extends readonly unknown[], | ||
| TypeToReturn, | ||
| > = | ||
| // If a function does not specify the `this` parameter, it will be inferred to `unknown` | ||
| IsUnknown<ThisParameter> extends true | ||
| ? (...args: Parameters_) => TypeToReturn | ||
| : (this: ThisParameter, ...args: Parameters_) => TypeToReturn; | ||
|
|
||
| export {}; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,109 @@ | ||||||||||||||||||||||||||||||
| import {expectType} from 'tsd'; | ||||||||||||||||||||||||||||||
| import type {FunctionOverloads} from '../source/function-overloads.d.ts'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| type Function1 = (foo: string, bar: number) => object; | ||||||||||||||||||||||||||||||
| type Function2 = (foo: bigint, ...bar: any[]) => void; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const normalFunction: FunctionOverloads<Function1>; | ||||||||||||||||||||||||||||||
| expectType<Function1>(normalFunction); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Note: function overload is equivalent to intersecting its signatures | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const twoOverloads: FunctionOverloads<Function1 & Function2>; | ||||||||||||||||||||||||||||||
| expectType<Function1 | Function2>(twoOverloads); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const twoIdenticalOverloads: FunctionOverloads<Function1>; | ||||||||||||||||||||||||||||||
| expectType<Function1>(twoIdenticalOverloads); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| type Function3 = (foo: string, bar: number, baz?: boolean) => object; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const twoOverloadsWithAssignableSignature: FunctionOverloads<Function1 & Function3>; | ||||||||||||||||||||||||||||||
| expectType<Function1 | Function3>(twoOverloadsWithAssignableSignature); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const threeOverloads: FunctionOverloads<Function1 & Function2 & Function3>; | ||||||||||||||||||||||||||||||
| expectType<Function1 | Function2 | Function3>(threeOverloads); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| type Function4 = (...foo: any[]) => void; | ||||||||||||||||||||||||||||||
| type Function5 = (...foo: readonly any[]) => void; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const normalFunctionWithOnlyRestWritableParameter: FunctionOverloads<Function4>; | ||||||||||||||||||||||||||||||
| expectType<Function4>(normalFunctionWithOnlyRestWritableParameter); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const normalFunctionWithOnlyRestReadonlyParameter: FunctionOverloads<Function5>; | ||||||||||||||||||||||||||||||
| expectType<Function5>(normalFunctionWithOnlyRestReadonlyParameter); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const twoOverloadsWithDifferentRestParameterReadonliness: FunctionOverloads< | ||||||||||||||||||||||||||||||
| Function4 & Function5 | ||||||||||||||||||||||||||||||
| >; | ||||||||||||||||||||||||||||||
| // Expected: it seems like the compiler ignores subsequent identical up to `readonly` modifier overloads | ||||||||||||||||||||||||||||||
| expectType<Function4>(twoOverloadsWithDifferentRestParameterReadonliness); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const twoOverloadsWithDifferentRestParameterReadonlinessReversed: FunctionOverloads< | ||||||||||||||||||||||||||||||
| Function5 & Function4 | ||||||||||||||||||||||||||||||
| >; | ||||||||||||||||||||||||||||||
| // Expected: it seems like the compiler ignores subsequent identical up to `readonly` modifier overloads | ||||||||||||||||||||||||||||||
| expectType<Function5>(twoOverloadsWithDifferentRestParameterReadonlinessReversed); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| type Function6 = (foo: string, ...bar: any[]) => void; | ||||||||||||||||||||||||||||||
| type Function7 = (foo: string, ...bar: readonly any[]) => void; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const normalFunctionWithNormalAndRestWritableParameter: FunctionOverloads<Function6>; | ||||||||||||||||||||||||||||||
| expectType<Function6>(normalFunctionWithNormalAndRestWritableParameter); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const normalFunctionWithNormalAndRestReadonlyParameter: FunctionOverloads<Function7>; | ||||||||||||||||||||||||||||||
| // Expected: readonly rest parameter cannot be represented with tuples | ||||||||||||||||||||||||||||||
| expectType<(foo: string, ...bar: any[]) => void>(normalFunctionWithNormalAndRestReadonlyParameter); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| type Function8 = () => never; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const normalFunctionNoParameters: FunctionOverloads<Function8>; | ||||||||||||||||||||||||||||||
| expectType<Function8>(normalFunctionNoParameters); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const twoOverloadsWithNoAndPresentParameters: FunctionOverloads<Function8 & Function6>; | ||||||||||||||||||||||||||||||
| expectType<Function8 | Function6>(twoOverloadsWithNoAndPresentParameters); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| type Function9 = (event: 'event9', arg: string) => void; | ||||||||||||||||||||||||||||||
| type Function10 = (event: 'event10', arg: number) => string; | ||||||||||||||||||||||||||||||
| type Function11 = (event: 'event11', arg: boolean) => never; | ||||||||||||||||||||||||||||||
| type Function12 = (event: 'event12', arg: bigint) => object; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const manyOverloads: FunctionOverloads< | ||||||||||||||||||||||||||||||
| Function1 & | ||||||||||||||||||||||||||||||
| Function2 & | ||||||||||||||||||||||||||||||
| Function3 & | ||||||||||||||||||||||||||||||
| Function4 & | ||||||||||||||||||||||||||||||
| Function5 & | ||||||||||||||||||||||||||||||
| Function6 & | ||||||||||||||||||||||||||||||
| Function7 & | ||||||||||||||||||||||||||||||
| Function8 & | ||||||||||||||||||||||||||||||
| Function9 & | ||||||||||||||||||||||||||||||
| Function10 & | ||||||||||||||||||||||||||||||
| Function11 & | ||||||||||||||||||||||||||||||
| Function12 | ||||||||||||||||||||||||||||||
| >; | ||||||||||||||||||||||||||||||
| expectType< | ||||||||||||||||||||||||||||||
| | Function1 | ||||||||||||||||||||||||||||||
| | Function2 | ||||||||||||||||||||||||||||||
| | Function3 | ||||||||||||||||||||||||||||||
| | Function4 | ||||||||||||||||||||||||||||||
| | Function5 | ||||||||||||||||||||||||||||||
| | Function6 | ||||||||||||||||||||||||||||||
| | Function7 | ||||||||||||||||||||||||||||||
| | Function8 | ||||||||||||||||||||||||||||||
| | Function9 | ||||||||||||||||||||||||||||||
| | Function10 | ||||||||||||||||||||||||||||||
| | Function11 | ||||||||||||||||||||||||||||||
| | Function12 | ||||||||||||||||||||||||||||||
| >(manyOverloads); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const noOverloads: FunctionOverloads<{}>; | ||||||||||||||||||||||||||||||
| expectType<never>(noOverloads); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const anyOverload: FunctionOverloads<any>; | ||||||||||||||||||||||||||||||
| expectType<(...arguments_: readonly unknown[]) => unknown>(anyOverload); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const neverOverload: FunctionOverloads<never>; | ||||||||||||||||||||||||||||||
| expectType<never>(neverOverload); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| declare const unknownOverload: FunctionOverloads<unknown>; | ||||||||||||||||||||||||||||||
| expectType<never>(unknownOverload); | ||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some test for
Suggested change
|
||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.