Skip to content

Commit 99082ac

Browse files
ChristianMurphywooorm
authored andcommitted
Add types
Closes GH-3. Reviewed-by: Titus Wormer <tituswormer@gmail.com> Reviewed-by: Junyoung Choi <fluke8259@gmail.com>
1 parent aeea007 commit 99082ac

File tree

5 files changed

+120
-3
lines changed

5 files changed

+120
-3
lines changed

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@
2020
"author": "Eugene Sharygin <eush77@gmail.com>",
2121
"contributors": [
2222
"Eugene Sharygin <eush77@gmail.com>",
23-
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
23+
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
24+
"Christian Murphy <christian.murphy.42@gmail.com>"
2425
],
2526
"files": [
26-
"index.js"
27+
"index.js",
28+
"types/index.d.ts"
2729
],
30+
"types": "types/index.d.ts",
2831
"dependencies": {
2932
"object-assign": "^4.1.0"
3033
},
3134
"devDependencies": {
35+
"@types/mdast": "^3.0.3",
36+
"dtslint": "^0.9.9",
3237
"nyc": "^14.0.0",
3338
"prettier": "^1.14.2",
3439
"remark-cli": "^6.0.0",
@@ -40,7 +45,8 @@
4045
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
4146
"test-api": "node test",
4247
"test-coverage": "nyc --reporter lcov tape test.js",
43-
"test": "npm run format && npm run test-coverage"
48+
"test-types": "dtslint types",
49+
"test": "npm run format && npm run test-coverage && npm run test-types"
4450
},
4551
"nyc": {
4652
"check-coverage": true,

types/index.d.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// TypeScript Version: 3.5
2+
3+
import {Node} from 'unist'
4+
5+
// NOTE: namespace is needed to use `export = unistBuilder`
6+
declare namespace unistBuilder {}
7+
8+
// NOTE: the order of the unistBuilder overloads is important.
9+
// Looking at the generics' "extends" left to right.
10+
// It should go from more specific types higher in the file, to more broad types lower in the file.
11+
12+
/**
13+
* Creates a node, with a given type
14+
*
15+
* @param type type of node
16+
*/
17+
declare function unistBuilder<T extends string>(type: T): {type: T}
18+
19+
/**
20+
* Creates a node, with type and value
21+
*
22+
* @param type type of node
23+
* @param value value property of node
24+
*/
25+
declare function unistBuilder<T extends string>(
26+
type: T,
27+
value: string
28+
): {type: T; value: string}
29+
30+
/**
31+
* Creates a node, with type, props, and value
32+
*
33+
* @param type type of node
34+
* @param props additional properties for node
35+
* @param value value property of node
36+
*/
37+
declare function unistBuilder<T extends string, P extends {}>(
38+
type: T,
39+
props: P,
40+
value: string
41+
): {type: T; value: string} & P
42+
43+
/**
44+
* Creates a node, with type and children
45+
*
46+
* @param type type of node
47+
* @param children child nodes of the current node
48+
*/
49+
declare function unistBuilder<T extends string, C extends Node[]>(
50+
type: T,
51+
children: C
52+
): {type: T; children: C}
53+
54+
/**
55+
* Creates a node, with type, props, and children
56+
*
57+
* @param type type of node
58+
* @param props additional properties for node
59+
* @param children child nodes of the current node
60+
*/
61+
declare function unistBuilder<T extends string, P extends {}, C extends Node[]>(
62+
type: T,
63+
props: P,
64+
children: C
65+
): {type: T; children: C} & P
66+
67+
/**
68+
* Creates a node, with type and props
69+
*
70+
* @param type type of node
71+
* @param props additional properties for node
72+
*/
73+
declare function unistBuilder<T extends string, P extends {}>(
74+
type: T,
75+
props: P
76+
): {type: T} & P
77+
78+
export = unistBuilder

types/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"unist-builder": ["index.d.ts"]
8+
}
9+
}
10+
}

types/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"whitespace": false,
5+
"semicolon": false
6+
}
7+
}

types/unist-builder-tests.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as u from 'unist-builder'
2+
import {Text, List, ListItem, HTML} from 'mdast'
3+
4+
u('example') // $ExpectType { type: "example"; }
5+
u('example', {property: true}) // $ExpectType { type: "example"; } & { property: boolean; }
6+
const node1 = u('text', 'text') // $ExpectType { type: "text"; value: string; }
7+
const text: Text = node1
8+
9+
// $ExpectType { type: "list"; children: { type: "listItem"; children: ({ type: "html"; value: string; } & { checked: boolean; })[]; }[]; }
10+
const node2 = u('list', [
11+
u('listItem', [u('html', {checked: true}, '<strong>text</strong>')])
12+
])
13+
14+
const list: List = node2
15+
const listItem: ListItem = node2.children[0]
16+
const html: HTML = node2.children[0].children[0]

0 commit comments

Comments
 (0)