mhkeller / indian-ocean

A library for reading in and writing out data (plus some useful filesystem functions) in Node.js.
https://mhkeller.github.io/indian-ocean/
MIT License
63 stars 14 forks source link

Typings #59

Open Rich-Harris opened 7 years ago

Rich-Harris commented 7 years ago

I have become utterly spoiled by TypeScript, and desperately miss libraries that don't have type declarations.

Short demo of what using indian-ocean would be like with types (note: this is in a JavaScript file, not a TypeScript file, in VSCode):

types

On line 6, it knows that readCsvSync is one of the methods available, so we get fuzzy autocomplete. If we got the signature wrong (i.e. first argument isn't a string) it would tell us about it. On line 8, it knows that csv is an array, so we get fuzzy autocomplete for forEach. On line 9, it knows that the value of raw.foo is a string, so we get fuzzy autocomplete for startsWith.

That was all made possible by adding a file called node_modules/indian-ocean/index.d.ts:

interface Datum {
  [key: string]: string;
}

export function readCsvSync (path: string): Datum[];

We'd basically just need to add a bunch of those for all the different readers etc. It doesn't even need to be perfect first time, as TypeScript is fairly forgiving (it'll never prevent anything from running, just might give you some red squiggles).

Whaddaya think?

mhkeller commented 7 years ago

Sounds great! csv soundsystem project?

Is there a way to roll this into the generated documentation too? The last bit of work I have on the rollup/es6 branch is to fix the doc generator that was broken by breaking things out into multiple files.

Rich-Harris commented 7 years ago

my thoughts exactly! tbd whether i'll make it tonight. not sure what the docs story is. I feel like it ought to be possible to do at least what JSDocs can do from typings, but really not sure.

mhkeller commented 7 years ago

Something like this? http://typedoc.org/guides/installation/

Rich-Harris commented 7 years ago

oh, interesting. worth trying for sure

mhkeller commented 7 years ago

After looking into it, I'm not super impressed with typedoc. Let's stick with documentationjs for now since it's got all the examples and things. I got it mostly working today pending some minor investigation into a change.

On this topic, declarations have begun to be merged so we'll just keep on adding them to the rollup branch (more aptly named would be 3.0.0 branch) but, as you said, they don't need to be complete before this gets merged to master.

mhkeller commented 6 years ago

FYI what has been implemented so far is now available in 3.0.0.

mhkeller commented 6 years ago

FYI#2, after some investigation, this also works in Sublime Text with the Typscript plugin. You have to set the syntax to Typescript though, which is easier with the help of this project-specific syntax plugin.