-
Notifications
You must be signed in to change notification settings - Fork 7
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
ES6 classes and Advanced Typings #13
base: master
Are you sure you want to change the base?
Changes from 43 commits
c277c8b
e31439c
45e2900
c7e3ab8
bd47956
704ec37
8eb3da2
56f3c40
8200e94
49fd9b1
f735d41
e5d90dc
7856502
f6d3ccb
af1775f
bc246ca
d9c20eb
fe8d724
c96f690
e9996bc
a1cd346
da39743
50b89aa
8d05297
c7f86c5
26a5acd
354bb22
6b0475c
110f520
f1df7d2
584213d
91dea6c
fef2240
f404d78
34618bf
d6727b9
07a5e1c
ffc1dc4
3fa75e0
95f1d95
a104b6d
afe85fa
7512984
0360843
d4deb25
c4c1624
b67c21f
fe245ef
30b3a3e
8b47161
4cc3178
c2bb931
b7639de
bada7a9
eb09e33
4741517
7b63b84
1951f38
1599dcd
8e07330
1ed72ef
831fa32
ed14d23
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 |
---|---|---|
|
@@ -36,4 +36,5 @@ jspm_packages | |
# Optional REPL history | ||
.node_repl_history | ||
|
||
/dist | ||
# dist | ||
dist | ||
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
declare module "callable-instance" { | ||
const CALL: unique symbol; | ||
export type SCALL = typeof CALL; | ||
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 think we should remove this type alias. It's not saving much and it adds an extra concept for people to be confused by. In this file we can use |
||
|
||
type BaseProperty = symbol | string | number; | ||
type CustomProperty = Exclude<BaseProperty, SCALL>; | ||
|
||
type BaseFunc = (...args: any) => any; | ||
type BaseClass = abstract new (...args: any) => any; | ||
type BaseInterface = { | ||
[k: BaseProperty]: any; | ||
}; | ||
|
||
type ExtractFuncFromInterface< | ||
I extends BaseInterface, | ||
P extends BaseProperty | ||
> = I[P] extends BaseFunc ? I[P] : never; | ||
|
||
interface CloneFuncFromClass<C extends BaseClass, P extends BaseProperty> { | ||
/** | ||
* For TS generics and function overload support use interface or function type for Callable | ||
*/ | ||
(...args: Parameters<InstanceType<C>[P]>): ReturnType<InstanceType<C>[P]>; | ||
} | ||
type ExtractFunc< | ||
C extends BaseClass | BaseFunc | BaseInterface, | ||
P extends BaseProperty | ||
> = C extends BaseClass | ||
? CloneFuncFromClass<C, P> | ||
: C extends BaseFunc | ||
? C | ||
: C extends BaseInterface | ||
? ExtractFuncFromInterface<C, P> | ||
: never; | ||
|
||
export interface CallableConstructor { | ||
get CALL(): SCALL; | ||
|
||
makeCallable<I extends BaseInterface, P extends CustomProperty>( | ||
object: I, | ||
property: P | ||
): PickProperties<I> & ExtractFuncFromInterface<I, P>; | ||
makeCallable<I extends BaseInterface>( | ||
object: I | ||
): PickProperties<I> & ExtractFuncFromInterface<I, SCALL>; | ||
|
||
clone<C extends BaseInterface & BaseFunc>(callableObject: C): C; | ||
|
||
new < | ||
C extends BaseClass | BaseFunc | BaseInterface, | ||
P extends CustomProperty | ||
>( | ||
property: P | ||
): ExtractFunc<C, P>; | ||
|
||
new <C extends BaseClass | BaseFunc | BaseInterface>(): ExtractFunc< | ||
C, | ||
SCALL | ||
>; | ||
} | ||
|
||
type PickProperties<Obj extends Record<BaseProperty, unknown>> = { | ||
[k in keyof Obj]: Obj[k]; | ||
}; | ||
|
||
export type OverrideCall<S extends BaseClass> = { | ||
new < | ||
C extends BaseClass | BaseFunc | BaseInterface, | ||
P extends BaseProperty = SCALL | ||
>( | ||
...args: ConstructorParameters<S> | ||
): Omit<PickProperties<InstanceType<S>>, P> & ExtractFunc<C, P>; | ||
} & S; | ||
|
||
const Callable: CallableConstructor; | ||
|
||
export default Callable; | ||
} |
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.
I don't think this was a meaningful change, either remove it or remove the useless comment.