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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(bindNodeCallback): Works on overloaded functions #6072
Changes from all 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { bindNodeCallback } from 'rxjs'; | ||
import { a, b, c, d, e, f, g, A, B, C, D, E, F, G } from '../helpers'; | ||
|
||
import * as fs from 'fs'; | ||
|
||
describe('works with overloads' , () => { | ||
const o = bindNodeCallback(fs.readFile); | ||
o('foo').subscribe(v => { | ||
v // $ExpectType Buffer | ||
}); | ||
o('foo', {encoding: 'ascii', flag: ''}).subscribe(v => { | ||
v // $ExpectType Buffer | ||
}); | ||
o('foo', {}).subscribe(v => { | ||
v // $ExpectType Buffer | ||
}); | ||
o('foo', {encoding: 'foo'}); // $ExpectError | ||
o(true); // $ExpectError | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,3 +276,99 @@ export type ValueFromNotification<T> = T extends { kind: 'N' | 'E' | 'C' } | |
export type Falsy = null | undefined | false | 0 | -0 | 0n | ''; | ||
|
||
export type TruthyTypesOf<T> = T extends Falsy ? never : T; | ||
|
||
/** | ||
* One of those magic types that can extract types from an | ||
* overloaded function. See https://github.com/ReactiveX/rxjs/issues/5942 | ||
* for more details | ||
*/ | ||
type OverloadedArgumentsAndReturnType<T> = T extends { | ||
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. 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. Wait... this is a type that is a "better 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 this is the magical overloaded function inference technique described at https://stackoverflow.com/questions/52760509/typescript-returntype-of-overloaded-function/52761156#52761156 |
||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
(...args: infer A3): infer R; | ||
(...args: infer A4): infer R; | ||
(...args: infer A5): infer R; | ||
(...args: infer A6): infer R; | ||
(...args: infer A7): infer R; | ||
(...args: infer A8): infer R; | ||
(...args: infer A9): infer R; | ||
(...args: infer A10): infer R; | ||
} | ||
? [A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8 | A9 | A10, R] | ||
: T extends { | ||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
(...args: infer A3): infer R; | ||
(...args: infer A4): infer R; | ||
(...args: infer A5): infer R; | ||
(...args: infer A6): infer R; | ||
(...args: infer A7): infer R; | ||
(...args: infer A8): infer R; | ||
(...args: infer A9): infer R; | ||
} | ||
? [A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8 | A9, R] | ||
: T extends { | ||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
(...args: infer A3): infer R; | ||
(...args: infer A4): infer R; | ||
(...args: infer A5): infer R; | ||
(...args: infer A6): infer R; | ||
(...args: infer A7): infer R; | ||
(...args: infer A8): infer R; | ||
} | ||
? [A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8, R] | ||
: T extends { | ||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
(...args: infer A3): infer R; | ||
(...args: infer A4): infer R; | ||
(...args: infer A5): infer R; | ||
(...args: infer A6): infer R; | ||
(...args: infer A7): infer R; | ||
} | ||
? [A1 | A2 | A3 | A4 | A5 | A6 | A7, R] | ||
: T extends { | ||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
(...args: infer A3): infer R; | ||
(...args: infer A4): infer R; | ||
(...args: infer A5): infer R; | ||
(...args: infer A6): infer R; | ||
} | ||
? [A1 | A2 | A3 | A4 | A5 | A6, R] | ||
: T extends { | ||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
(...args: infer A3): infer R; | ||
(...args: infer A4): infer R; | ||
(...args: infer A5): infer R; | ||
} | ||
? [A1 | A2 | A3 | A4 | A5, R] | ||
: T extends { | ||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
(...args: infer A3): infer R; | ||
(...args: infer A4): infer R; | ||
} | ||
? [A1 | A2 | A3 | A4, R] | ||
: T extends { | ||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
(...args: infer A3): infer R; | ||
} | ||
? [A1 | A2 | A3, R] | ||
: T extends { | ||
(...args: infer A1): infer R; | ||
(...args: infer A2): infer R; | ||
} | ||
? [A1 | A2, R] | ||
: T extends (...args: infer A) => infer R | ||
? [A, R] | ||
: never; | ||
|
||
export type OverloadedParameters<T> = OverloadedArgumentsAndReturnType<T>[0]; | ||
export type OverloadedReturnType<T> = OverloadedArgumentsAndReturnType<T>[1]; | ||
|
||
export type AllButLast<T extends any[]> = T extends T ? (T extends [...(infer H), any] ? H : never) : never; | ||
export type Last<T extends any[]> = [any, ...T][T['length']]; |
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.
@benlesh Completely off topic, but shouldn't this include
NaN
?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.
Yeah, probably.
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.
NaN
is not a type; it's a value, AFAICT;number
is the type.