spencermountain / compromise

modest natural-language processing

Home Page:http://compromise.cool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improve TypeScript DX by reducing usage of "any"

simonbrunel opened this issue · comments

I'm using compromise with TypeScript and I see myself casting quite often the output of this library (i.e. as string[]):

(nlp(input).sentences().out("array") as string[]).map((v) => process(v));
(nlp(input).sentences().map((doc) => process(doc.text())) as string[]);

This is caused by the use of a few any, that slightly degrades the TypeScript developer experience. I believe this could be improved, for example by using overloads and generics:

declare class View {
  out(format?: 'text' | 'normal' | 'root' | 'machine' | 'reduced' | 'hash' | 'md5'): string
  out(format: 'array'): string[]
  out(format: 'debug'): View
  // ...
}
declare class View {
  map: <T>(fn: (m: View) => T, emptyResult?: T) => T extends View ? View : T[]
}

hey, thanks Simon - sorry for the delay.
Good idea! A PR is welcomed and really appreciated. I'm a klutz with typescript, and would really appreciate the help.
cheers