spencermountain/compromise

Improve TypeScript DX by reducing usage of "any"

simonbrunel opened this issue · 1 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