TL;DR I want to create TypeScript typings for compiled PureScript modules, and distribute them in my npm package. I'm more than happy to maintain these typings manually, I just can't figure out what I need to put in tsconfig.json (up and downstream) and package.json.
.
├── cli # TypeScript CLI project
│ ├── main.ts
│ └── tsconfig.json
│
├── output # Compiled PureScript modules
│ └── People
│ ├── externs.json
│ └── index.js
│
├── src # PureScript source
│ └── People.purs
│
└── types # TypeScript types for PureScript modules
└── People.d.ts
src/People.purs
module People where
type Person = { name :: String }
david :: Person
david = { name: "David "}
types/People.d.ts
declare module "People" {
export interface Person {
name: string;
}
export const david: Person;
}
cli/main.ts
import * as People from "People";
console.log(People.david);
node cli/main.js
require
tsc
require("People")
require("./People")
import
require
Declaring namespaces rather than modules makes everything work. For example, in types/People.d.ts
:
declare namespace People {
export interface Person {
name: string;
}
export const david: Person;
}
export = People;
export as namespace People;
After compiling PureScript modules and before compiling TypeScript, I cp types/People.d.ts output/People/index.d.ts
. This makes the TypeScript code happy with the same absolute imports (e.g. import * as People from "People";
), and TypeScript libraries also see these types without additional configuration.
A few issues remain, though:
../
in a post-build step); consumers of my library don't have to do this, though, since it goes through npm
and that magically makes it work.Data.Maybe
are represented by namespaces like Data_Maybe
.