Skip to content

Commit c48b691

Browse files
pimpalesanjayaksaxena
authored andcommitted
fix type declaration bugs, add a few more tests
1 parent 513b75c commit c48b691

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@
5353
"nyc": "^15.1.0"
5454
},
5555
"runkitExampleFilename": "./runkit-example.js",
56-
"types": "types"
56+
"types": "types/index.d.ts"
5757
}

types/index.d.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,14 @@ 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;
123-
markedUpText(tokens: any[]): string;
124122
}
125123

126124
// functions for use with document
127-
export type TokenItsFunction<OutType> = (index: number, token: Token, cache?: Cache, addons?: ModelAddons) => OutType;
128-
export type SpanItsFunction<OutType> = (spanItem?: number[]) => OutType;
129-
export type VectorizerItsFunction<OutType> = (tf?: ModelTermFrequencies, idf?: ModelInverseDocumentFrequencies) => OutType;
125+
export type TokenItsFunction<OutType> = (index: number, token: Token, cache: Cache, addons: ModelAddons) => OutType;
126+
export type SpanItsFunction<OutType> = (spanItem: number[]) => OutType;
127+
export type VectorizerItsFunction<OutType> = (tf: ModelTermFrequencies, idf: ModelInverseDocumentFrequencies) => OutType;
130128
export type ItsFunction<OutType> = TokenItsFunction<OutType> | SpanItsFunction<OutType> | VectorizerItsFunction<OutType>;
129+
131130
export type AsFunction<InType, OutType> = (tokens: InType[]) => OutType;
132131

133132
export interface ItemToken {
@@ -225,7 +224,7 @@ declare module 'wink-nlp' {
225224
parentDocument(): Document;
226225
markup(beginMarker: string, endMarker: string): void;
227226
out(): string;
228-
out<T>(itsf: ItsFunction<T>): T[] | string;
227+
out<T>(itsf: ItsFunction<T>): T | string;
229228
entities(): Entities;
230229
customEntities(): CustomEntities;
231230
tokens(): Tokens;
@@ -247,7 +246,7 @@ declare module 'wink-nlp' {
247246
isLexeme(text: string): boolean;
248247
isOOV(text: string): boolean;
249248
out(): string;
250-
out<T>(itsf: ItsFunction<T>): T[] | string;
249+
out<T>(itsf: ItsFunction<T>): T | string;
251250
sentences(): Sentences;
252251
tokens(): Tokens;
253252
printTokens(): void;

types/test.ts

+45-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import winkNlp, {
2-
Sentences,
3-
CustomEntities,
4-
SelectedCustomEntities,
5-
Entities,
6-
SelectedEntities,
7-
Tokens,
8-
SelectedTokens,
9-
ItsFunction,
10-
AsFunction,
2+
Sentences,
3+
CustomEntities,
4+
SelectedCustomEntities,
5+
Entities,
6+
SelectedEntities,
7+
Tokens,
8+
SelectedTokens,
9+
ItemCustomEntity,
10+
ItemEntity,
11+
ItemSentence,
12+
ItemToken,
13+
Document,
14+
ItsFunction,
15+
AsFunction,
1116
} from 'wink-nlp';
1217
import model from 'wink-eng-lite-web-model';
1318

@@ -35,9 +40,37 @@ customEntities.out(its.value, as.array);
3540
// $ExpectType string[]
3641
customEntities.out(its.value, as.array);
3742

38-
type OutApplicable = Sentences | CustomEntities | SelectedCustomEntities | Entities | SelectedEntities | Tokens | SelectedTokens;
43+
type ColOutApplicable = Sentences | CustomEntities | SelectedCustomEntities | Entities | SelectedEntities | Tokens | SelectedTokens;
3944

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 {
45+
// collection out
46+
// $ExpectType <T, U>(toOut: ColOutApplicable, itsf: ItsFunction<T>, asf: AsFunction<T, U>) => U
47+
function myColOut<T, U>(toOut: ColOutApplicable, itsf: ItsFunction<T>, asf: AsFunction<T, U>): U {
4248
return (toOut.out(itsf, asf) as any) as U;
4349
}
50+
51+
// $ExpectType string[] | boolean[] | [token: boolean, freq: number][]
52+
customEntities.out(its.contractionFlag, as.freqTable);
53+
54+
// $ExpectType [token: boolean, freq: number][]
55+
myColOut(customEntities, its.contractionFlag, as.freqTable);
56+
57+
// $ExpectType string[]
58+
doc.tokens().out();
59+
60+
// $ExpectType string[] | boolean[]
61+
doc.tokens().out(its.abbrevFlag);
62+
63+
// run out on item types
64+
type ItemOutApplicable = ItemCustomEntity | ItemEntity | ItemSentence | ItemToken | Document;
65+
66+
// item out
67+
// $ExpectType <T>(toOut: ItemOutApplicable, itsf: ItsFunction<T>) => T
68+
function myItemOut<T>(toOut: ItemOutApplicable, itsf: ItsFunction<T>): T {
69+
return (toOut.out(itsf) as any) as T;
70+
}
71+
72+
// $ExpectType string | number[]
73+
doc.out(its.span);
74+
75+
// $ExpectType number[]
76+
myItemOut(doc, its.span);

0 commit comments

Comments
 (0)