Skip to content

Commit 513b75c

Browse files
pimpalesanjayaksaxena
authored andcommitted
better handling of types
1 parent fe6e56e commit 513b75c

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

types/index.d.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ declare module 'wink-nlp' {
119119
freqTable<T>(tokens: T[]): Array<[token: T, freq: number]>;
120120
bigrams<T>(tokens: T[]): Array<[T, T]>;
121121
unique<T>(tokens: T[]): T[];
122+
text(tokens: any[]): string;
122123
markedUpText(tokens: any[]): string;
123124
}
124125

@@ -134,7 +135,8 @@ declare module 'wink-nlp' {
134135
parentEntity(): ItemEntity | undefined;
135136
parentCustomEntity(): ItemCustomEntity | undefined;
136137
markup(beginMarker: string, endMarker: string): void;
137-
out<T>(itsf: ItsFunction<T>): T;
138+
out(): string;
139+
out<T>(itsf: ItsFunction<T>): T | string;
138140
parentSentence(): ItemSentence;
139141
index(): number;
140142
}
@@ -144,21 +146,26 @@ declare module 'wink-nlp' {
144146
filter(f: (token: ItemToken) => boolean): SelectedTokens;
145147
itemAt(k: number): ItemToken | undefined;
146148
length(): number;
147-
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U;
149+
out(): string[];
150+
out<T>(itsf: ItsFunction<T>): T[] | string[];
151+
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U | T[] | string[];
148152
}
149153

150154
export interface Tokens {
151155
each(f: (token: ItemToken) => void): void;
152156
filter(f: (token: ItemToken) => boolean): SelectedTokens;
153157
itemAt(k: number): ItemToken | undefined;
154158
length(): number;
155-
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U;
159+
out(): string[];
160+
out<T>(itsf: ItsFunction<T>): T[] | string[];
161+
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U | T[] | string[];
156162
}
157163

158164
export interface ItemEntity {
159165
parentDocument(): Document;
160166
markup(beginMarker: string, endMarker: string): void;
161-
out<T>(itsf: ItsFunction<T>): T;
167+
out(): string;
168+
out<T>(itsf: ItsFunction<T>): T | string;
162169
parentSentence(): ItemSentence;
163170
tokens(): Tokens;
164171
index(): number;
@@ -169,21 +176,26 @@ declare module 'wink-nlp' {
169176
filter(f: (entity: ItemEntity) => boolean): SelectedEntities;
170177
itemAt(k: number): ItemEntity | undefined;
171178
length(): number;
172-
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U;
179+
out(): string[];
180+
out<T>(itsf: ItsFunction<T>): T[] | string[];
181+
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U | T[] | string[];
173182
}
174183

175184
export interface Entities {
176185
each(f: (entity: ItemEntity) => void): void;
177186
filter(f: (entity: ItemEntity) => boolean): SelectedEntities;
178187
itemAt(k: number): ItemEntity | undefined;
179188
length(): number;
180-
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U;
189+
out(): string[];
190+
out<T>(itsf: ItsFunction<T>): T[] | string[];
191+
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U | T[] | string[];
181192
}
182193

183194
export interface ItemCustomEntity {
184195
parentDocument(): Document;
185196
markup(beginMarker: string, endMarker: string): void;
186-
out<T>(itsf: ItsFunction<T>): T;
197+
out(): string;
198+
out<T>(itsf: ItsFunction<T>): T | string;
187199
parentSentence(): ItemSentence;
188200
tokens(): Tokens;
189201
index(): number;
@@ -194,21 +206,26 @@ declare module 'wink-nlp' {
194206
filter(f: (entity: ItemCustomEntity) => boolean): SelectedCustomEntities;
195207
itemAt(k: number): ItemCustomEntity | undefined;
196208
length(): number;
197-
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U;
209+
out(): string[];
210+
out<T>(itsf: ItsFunction<T>): T[] | string[];
211+
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U | T[] | string[];
198212
}
199213

200214
export interface CustomEntities {
201215
each(f: (entity: ItemCustomEntity) => void): void;
202216
filter(f: (entity: ItemCustomEntity) => boolean): SelectedCustomEntities;
203217
itemAt(k: number): ItemCustomEntity | undefined;
204218
length(): number;
205-
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U;
219+
out(): string[];
220+
out<T>(itsf: ItsFunction<T>): T[] | string[];
221+
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U | T[] | string[];
206222
}
207223

208224
export interface ItemSentence {
209225
parentDocument(): Document;
210226
markup(beginMarker: string, endMarker: string): void;
211-
out<T>(itsf: ItsFunction<T>): T;
227+
out(): string;
228+
out<T>(itsf: ItsFunction<T>): T[] | string;
212229
entities(): Entities;
213230
customEntities(): CustomEntities;
214231
tokens(): Tokens;
@@ -219,15 +236,18 @@ declare module 'wink-nlp' {
219236
each(f: (entity: ItemSentence) => void): void;
220237
itemAt(k: number): ItemSentence | undefined;
221238
length(): number;
222-
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U;
239+
out(): string[];
240+
out<T>(itsf: ItsFunction<T>): T[] | string[];
241+
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U | T[] | string[];
223242
}
224243

225244
export interface Document {
226245
entities(): Entities;
227246
customEntities(): CustomEntities;
228247
isLexeme(text: string): boolean;
229248
isOOV(text: string): boolean;
230-
out<T>(itsf: ItsFunction<T>): T;
249+
out(): string;
250+
out<T>(itsf: ItsFunction<T>): T[] | string;
231251
sentences(): Sentences;
232252
tokens(): Tokens;
233253
printTokens(): void;

types/test.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1-
import winkNlp from 'wink-nlp';
1+
import winkNlp, {
2+
Sentences,
3+
CustomEntities,
4+
SelectedCustomEntities,
5+
Entities,
6+
SelectedEntities,
7+
Tokens,
8+
SelectedTokens,
9+
ItsFunction,
10+
AsFunction,
11+
} from 'wink-nlp';
212
import model from 'wink-eng-lite-web-model';
313

414
// $ExpectType WinkMethods
515
winkNlp(model);
616

717
// $ExpectType WinkMethods
8-
winkNlp(model, ["foo"]);
18+
const nlp = winkNlp(model, ["foo"]);
919

1020
// $ExpectType ItsHelpers
11-
winkNlp(model).its;
21+
const its = nlp.its;
1222

1323
// $ExpectType AsHelpers
14-
winkNlp(model).as;
24+
const as = winkNlp(model).as;
25+
26+
// $ExpectType Document
27+
const doc = nlp.readDoc('test');
28+
29+
// $ExpectType CustomEntities
30+
const customEntities = doc.customEntities();
31+
32+
// $ExpectType string[]
33+
customEntities.out(its.value, as.array);
34+
35+
// $ExpectType string[]
36+
customEntities.out(its.value, as.array);
37+
38+
type OutApplicable = Sentences | CustomEntities | SelectedCustomEntities | Entities | SelectedEntities | Tokens | SelectedTokens;
39+
40+
// $ExpectType <T, U>(toOut: OutApplicable, itsf: ItsFunction<T>, asf: AsFunction<T, U>) => U
41+
function myOut<T, U>(toOut: OutApplicable, itsf: ItsFunction<T>, asf: AsFunction<T, U>): U {
42+
return (toOut.out(itsf, asf) as any) as U;
43+
}

0 commit comments

Comments
 (0)