1
1
import {
2
- hasProperty ,
3
2
UnionToIntersection ,
4
3
Version ,
5
4
} from "./_namespaces/ts" ;
6
- import {
7
- deprecate ,
8
- } from "./deprecate" ;
9
5
10
6
/** @internal */
11
7
export interface DeprecationOptions {
@@ -37,9 +33,6 @@ export interface DeprecationOptions {
37
33
*/
38
34
export type OverloadDefinitions = { readonly [ P in number ] : ( ...args : any [ ] ) => any ; } ;
39
35
40
- /** A function that returns the ordinal of the overload that matches the provided arguments */
41
- type OverloadBinder < T extends OverloadDefinitions > = ( args : OverloadParameters < T > ) => OverloadKeys < T > | undefined ;
42
-
43
36
/**
44
37
* Extracts the ordinals from an set of overload definitions.
45
38
*
@@ -79,43 +72,6 @@ export type OverloadBinders<T extends OverloadDefinitions> = { [P in OverloadKey
79
72
*/
80
73
export type OverloadDeprecations < T extends OverloadDefinitions > = { [ P in OverloadKeys < T > ] ?: DeprecationOptions ; } ;
81
74
82
- /** @internal */
83
- function createOverload < T extends OverloadDefinitions > ( name : string , overloads : T , binder : OverloadBinders < T > , deprecations ?: OverloadDeprecations < T > ) {
84
- Object . defineProperty ( call , "name" , { ...Object . getOwnPropertyDescriptor ( call , "name" ) , value : name } ) ;
85
-
86
- if ( deprecations ) {
87
- for ( const key of Object . keys ( deprecations ) ) {
88
- const index = + key as ( keyof T & number ) ;
89
- if ( ! isNaN ( index ) && hasProperty ( overloads , `${ index } ` ) ) {
90
- overloads [ index ] = deprecate ( overloads [ index ] , { ...deprecations [ index ] , name } ) ;
91
- }
92
- }
93
- }
94
-
95
- const bind = createBinder ( overloads , binder ) ;
96
- return call as OverloadFunction < T > ;
97
-
98
- function call ( ...args : OverloadParameters < T > ) {
99
- const index = bind ( args ) ;
100
- const fn = index !== undefined ? overloads [ index ] : undefined ;
101
- if ( typeof fn === "function" ) {
102
- return fn ( ...args ) ;
103
- }
104
- throw new TypeError ( "Invalid arguments" ) ;
105
- }
106
- }
107
-
108
- function createBinder < T extends OverloadDefinitions > ( overloads : T , binder : OverloadBinders < T > ) : OverloadBinder < T > {
109
- return args => {
110
- for ( let i = 0 ; hasProperty ( overloads , `${ i } ` ) && hasProperty ( binder , `${ i } ` ) ; i ++ ) {
111
- const fn = binder [ i ] ;
112
- if ( fn ( args ) ) {
113
- return i as OverloadKeys < T > ;
114
- }
115
- }
116
- } ;
117
- }
118
-
119
75
/** @internal */
120
76
export interface OverloadBuilder {
121
77
overload < T extends OverloadDefinitions > ( overloads : T ) : BindableOverloadBuilder < T > ;
@@ -135,20 +91,3 @@ export interface FinishableOverloadBuilder<T extends OverloadDefinitions> {
135
91
export interface BoundOverloadBuilder < T extends OverloadDefinitions > extends FinishableOverloadBuilder < T > {
136
92
deprecate ( deprecations : OverloadDeprecations < T > ) : FinishableOverloadBuilder < T > ;
137
93
}
138
-
139
- // NOTE: We only use this "builder" because we don't infer correctly when calling `createOverload` directly in < TS 4.7,
140
- // but lib is currently at TS 4.4. We can switch to directly calling `createOverload` when we update LKG in main.
141
-
142
- /** @internal */
143
- function buildOverload ( name : string ) : OverloadBuilder {
144
- return {
145
- overload : overloads => ( {
146
- bind : binder => ( {
147
- finish : ( ) => createOverload ( name , overloads , binder ) ,
148
- deprecate : deprecations => ( {
149
- finish : ( ) => createOverload ( name , overloads , binder , deprecations ) ,
150
- } ) ,
151
- } ) ,
152
- } ) ,
153
- } ;
154
- }
0 commit comments