-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
[@types/lodash] Infer lodash.throttle return type from 'leading' option #69504
Changes from 2 commits
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 |
---|---|---|
|
@@ -3656,25 +3656,56 @@ fp.now(); // $ExpectType number | |
|
||
// _.throttle | ||
{ | ||
const options: _.ThrottleSettings = { | ||
const optionsLeading: _.ThrottleSettingsLeading = { | ||
leading: true, | ||
trailing: false, | ||
} | ||
const badOptionsLeading: _.ThrottleSettingsLeading = { | ||
// @ts-expect-error lodash will treat an explicit undefined the same as `leading: false` | ||
leading: undefined, | ||
}; | ||
const optionsNoLeading: _.ThrottleSettings = { | ||
leading: false, | ||
}; | ||
|
||
const optionsAmbiguous = { | ||
leading: true, | ||
}; | ||
const boolean: boolean = false; | ||
const maybeUndefined = (function (): true | undefined { return true })(); | ||
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. Similar to above: a simpler way to do this might be something like: const maybeUndefined: true | undefined = true as any; |
||
|
||
const func = (a: number, b: string): boolean => true; | ||
|
||
_.throttle(func); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
_.throttle(func, 42); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
_.throttle(func, 42, options); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
_(func).throttle(); // $ExpectType Function<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_(func).throttle(42); // $ExpectType Function<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_(func).throttle(42, options); // $ExpectType Function<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(); // $ExpectType FunctionChain<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42); // $ExpectType FunctionChain<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42, options); // $ExpectType FunctionChain<DebouncedFunc<(a: number, b: string) => boolean>> | ||
|
||
fp.throttle(42, func); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
fp.throttle(42)(func); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
_.throttle(func); // $ExpectType DebouncedFuncLeading<(a: number, b: string) => boolean> | ||
_.throttle(func, 42); // $ExpectType DebouncedFuncLeading<(a: number, b: string) => boolean> | ||
_.throttle(func, 42, {}); // $ExpectType DebouncedFuncLeading<(a: number, b: string) => boolean> | ||
_.throttle(func, 42, { leading: true, trailing: false }); // $ExpectType DebouncedFuncLeading<(a: number, b: string) => boolean> | ||
_.throttle(func, 42, { leading: false }); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
_.throttle(func, 42, { leading: undefined }); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
_.throttle(func, 42, { leading: boolean }); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
_.throttle(func, 42, { leading: maybeUndefined }); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
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. Also add a comment explaining why we should NOT expect |
||
_.throttle(func, 42, optionsAmbiguous); // $ExpectType DebouncedFunc<(a: number, b: string) => boolean> | ||
_(func).throttle(); // $ExpectType Function<DebouncedFuncLeading<(a: number, b: string) => boolean>> | ||
_(func).throttle(42); // $ExpectType Function<DebouncedFuncLeading<(a: number, b: string) => boolean>> | ||
_(func).throttle(42, {}); // $ExpectType Function<DebouncedFuncLeading<(a: number, b: string) => boolean>> | ||
_(func).throttle(42, { leading: true, trailing: false }); // $ExpectType Function<DebouncedFuncLeading<(a: number, b: string) => boolean>> | ||
_(func).throttle(42, { leading: false }); // $ExpectType Function<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_(func).throttle(42, { leading: undefined }); // $ExpectType Function<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_(func).throttle(42, { leading: boolean }); // $ExpectType Function<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_(func).throttle(42, { leading: maybeUndefined }); // $ExpectType Function<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_(func).throttle(42, optionsAmbiguous); // $ExpectType Function<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(); // $ExpectType FunctionChain<DebouncedFuncLeading<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42); // $ExpectType FunctionChain<DebouncedFuncLeading<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42, {}); // $ExpectType FunctionChain<DebouncedFuncLeading<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42, { leading: true, trailing: false }); // $ExpectType FunctionChain<DebouncedFuncLeading<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42, { leading: false }); // $ExpectType FunctionChain<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42, { leading: undefined }); // $ExpectType FunctionChain<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42, { leading: boolean }); // $ExpectType FunctionChain<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42, { leading: maybeUndefined }); // $ExpectType FunctionChain<DebouncedFunc<(a: number, b: string) => boolean>> | ||
_.chain(func).throttle(42, optionsAmbiguous); // $ExpectType FunctionChain<DebouncedFunc<(a: number, b: string) => boolean>> | ||
|
||
fp.throttle(42, func); // $ExpectType DebouncedFuncLeading<(a: number, b: string) => boolean> | ||
fp.throttle(42)(func); // $ExpectType DebouncedFuncLeading<(a: number, b: string) => boolean> | ||
} | ||
|
||
// _.unary | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you might need
false as boolean
, otherwise TS will automatically narrow the type tofalse
which is not what we want.