Skip to content

Add JSDoc based types #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tsd
  • Loading branch information
wooorm committed Apr 18, 2021
commit 74499a68366e0524a87925605243d18e2495328d
35 changes: 7 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,16 @@
* @typedef {Object.<string, unknown>} Props
* @typedef {Array.<Node>|string} ChildrenOrValue
*
* @callback BuildParentWithProps
* @param {string} type
* @param {Props} props
* @param {Array.<Node>} value
* @returns {Parent}
*
* @callback BuildParentWithoutProps
* @param {string} type
* @param {Array.<Node>} value
* @returns {Parent}
*
* @callback BuildLiteralWithProps
* @param {string} type
* @param {Props} props
* @param {string} value
* @returns {Literal}
*
* @callback BuildLiteralWithoutProps
* @param {string} type
* @param {string} value
* @returns {Literal}
*
* @callback BuildVoid
* @param {string} type
* @param {Props} [props]
* @returns {Node}
* @typedef {(<T extends string, P extends Record<string, unknown>, C extends Node[]>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps
* @typedef {(<T extends string, C extends Node[]>(type: T, children: C) => {type: T, children: C})} BuildParent
* @typedef {(<T extends string>(type: T, value: string) => {type: T, value: string})} BuildLiteral
* @typedef {(<T extends string>(type: T) => {type: T})} BuildVoid
*/

/**
* @type {BuildParentWithProps & BuildParentWithoutProps & BuildLiteralWithProps & BuildLiteralWithoutProps & BuildVoid}
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps}
*/
// prettier-ignore
export var u = (
Expand Down Expand Up @@ -65,5 +45,4 @@ export var u = (

return node
}

)
26 changes: 26 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {expectType, expectAssignable} from 'tsd'
import {Text, List, ListItem, HTML} from 'mdast'
import {u} from './index.js'

expectType<{type: 'example'}>(u('example'))
expectType<{type: 'example'} & {property: true}>(u('example', {property: true}))

const node1 = u('text', 'text')

expectType<{type: 'text'; value: string}>(node1)
expectAssignable<Text>(node1)

const node2 = u('list', [
u('listItem', [u('html', {checked: true}, '<strong>text</strong>')])
])

expectType<{
type: 'list'
children: Array<{
type: 'listItem'
children: Array<{type: 'html'; value: string} & {checked: boolean}>
}>
}>(node2)
expectAssignable<List>(node2)
expectAssignable<ListItem>(node2.children[0])
expectAssignable<HTML>(node2.children[0].children[0])
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"tsd": "^0.14.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"build": "rimraf \"*.d.ts\" && tsc && tsd && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
Expand Down