Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type TraphConfig<I,O> = {
[K in keyof O]: (i: I, o: O) => O[K]
}

interface TraphTransformer<I,O> {
(input: I): O
lazy: (input: I) => O
}

declare function traph<I = Record<string,any>,O = Record<string,any>>(config: TraphConfig<I,O>): TraphTransformer<I,O>

export default traph
23 changes: 23 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expectType, expectAssignable, expectError } from "tsd"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my god, I missed this library completely... I'm losing my edge

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

import traph from "."

type Input = { in: number[] }
type Output = { out: number }

const stats = traph<Input, Output>({
out: (i,o) => {
expectType<Input>(i)
expectType<Output>(o)
return i.in.length
}
})

expectAssignable<(i: Input) => Output>(stats)
expectType<(i: Input) => Output>(stats.lazy)

declare const data: Input
expectType<Output>(stats(data))

expectError(traph<Input,Output>({}))
expectError(traph<Input,Output>({out: "string value"}))
expectError(traph<Input,Output>({out: (i,o) => "string value"}))
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
"lint": "eslint index.js",
"test:watch": "ava --watch",
"test": "npm run lint && ava",
"prepublish": "npm run build && npm test"
"test:types": "tsd",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could we call both ava and tsd with the yarn test and yarn test:watch scripts?

Copy link
Author

@ivanross ivanross May 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I get what you mean. Are you wondering how to call both ava and tsd in watch mode at the same time with one call? However, a "watch" option seems to be missing in tsd

"prepublish": "npm run build && npm test && npm run test:types"
},
"main": "dist/index.js",
"files": [
"dist"
"dist",
"index.d.ts"
],
"type": "index.d.ts",
"ava": {
"require": [
"babel-register"
Expand All @@ -31,7 +34,8 @@
"eslint-config-standard": "7.0.0-beta.0",
"eslint-plugin-ava": "^4.1.0",
"eslint-plugin-promise": "^3.4.1",
"eslint-plugin-standard": "^2.0.1"
"eslint-plugin-standard": "^2.0.1",
"tsd": "^0.11.0"
},
"dependencies": {}
}
Loading