Skip to content

Commit

Permalink
Add partial interface exports to types
Browse files Browse the repository at this point in the history
Closes GH-457.

Reviewed-by: Christian Murphy <[email protected]>
Reviewed-by: Victor Felder <[email protected]>
Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
Roang-zero1 authored and wooorm committed Dec 9, 2019
1 parent de3de6d commit 24cd52d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/remark-parse/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ declare class RemarkParser implements Parser {
}

declare namespace remarkParse {
interface Parse extends Plugin<[Partial<RemarkParseOptions>?]> {
(options: Partial<RemarkParseOptions>): void
interface Parse extends Plugin<[PartialRemarkParseOptions?]> {
(options: PartialRemarkParseOptions): void
Parser: typeof RemarkParser
}

Expand All @@ -28,6 +28,8 @@ declare namespace remarkParse {
pedantic: boolean
}

type PartialRemarkParseOptions = Partial<RemarkParseOptions>

interface Add {
(node: Node, parent?: Parent): Node
test(): Position
Expand Down
15 changes: 13 additions & 2 deletions packages/remark-parse/types/test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import unified = require('unified')
import remarkParse = require('remark-parse')

const parseOptions = {
const partialParseOptions: remarkParse.PartialRemarkParseOptions = {
gfm: true,
pedantic: true
}

unified().use(remarkParse)
unified().use(remarkParse, parseOptions)
unified().use(remarkParse, partialParseOptions)

const badParseOptions = {
gfm: 'true'
}

// $ExpectError
unified().use(remarkParse, badParseOptions)

// $ExpectError
const parseOptions: remarkParse.RemarkParseOptions = {
gfm: true,
pedantic: true
}

const badParseOptionsInterface: remarkParse.PartialRemarkParseOptions = {
// $ExpectError
gfm: 'true'
}
6 changes: 4 additions & 2 deletions packages/remark-stringify/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ declare class RemarkCompiler implements Compiler {
}

declare namespace remarkStringify {
interface Stringify extends Plugin<[Partial<RemarkStringifyOptions>?]> {
interface Stringify extends Plugin<[PartialRemarkStringifyOptions?]> {
Compiler: typeof RemarkCompiler
(this: Processor, options?: Partial<RemarkStringifyOptions>): void
(this: Processor, options?: PartialRemarkStringifyOptions): void
}

type Compiler = RemarkCompiler
Expand All @@ -40,6 +40,8 @@ declare namespace remarkStringify {
emphasis: '_' | '*'
}

type PartialRemarkStringifyOptions = Partial<RemarkStringifyOptions>

type Visitor = (node: Node, parent?: Parent) => string
}

Expand Down
15 changes: 14 additions & 1 deletion packages/remark-stringify/types/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import remarkStringify = require('remark-stringify')
import unified = require('unified')

import {Node, Parent} from 'unist'

const inferredStringifyOptions = {
Expand All @@ -12,7 +13,7 @@ unified().use(remarkStringify)
unified().use(remarkStringify, inferredStringifyOptions)

// These cannot be automatically inferred by TypeScript
const nonInferredStringifyOptions: Partial<remarkStringify.RemarkStringifyOptions> = {
const nonInferredStringifyOptions: remarkStringify.PartialRemarkStringifyOptions = {
fence: '~',
bullet: '+',
listItemIndent: 'tab',
Expand Down Expand Up @@ -48,3 +49,15 @@ function gap(this: unified.Processor) {
}

const plugin: unified.Plugin = gap

const badPartialStringifyOptions: remarkStringify.PartialRemarkStringifyOptions = {
// $ExpectError
gfm: 'true'
}

// $ExpectError
const incompleteStringifyOptions: remarkStringify.RemarkStringifyOptions = {
gfm: true,
fences: true,
incrementListMarker: false
}
7 changes: 4 additions & 3 deletions packages/remark/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import remarkStringify = require('remark-stringify')
declare namespace remark {
type RemarkOptions = remarkParse.RemarkParseOptions &
remarkStringify.RemarkStringifyOptions

type PartialRemarkOptions = remarkParse.PartialRemarkParseOptions &
remarkStringify.PartialRemarkStringifyOptions
}

declare function remark<
P extends Partial<remark.RemarkOptions> = Partial<remark.RemarkOptions>
>(): unified.Processor<P>
declare function remark(): unified.Processor<remark.PartialRemarkOptions>

export = remark
11 changes: 11 additions & 0 deletions packages/remark/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ remark().use({settings: {commonmark: true}})
remark().use({settings: {doesNotExist: true}})
// $ExpectError
remark().use(plugin, {doesNotExist: 'true'})

// $ExpectError
const parseOptions: remark.RemarkOptions = {
gfm: true,
pedantic: true
}

const badParseOptionsInterface: remark.PartialRemarkOptions = {
// $ExpectError
gfm: 'true'
}

0 comments on commit 24cd52d

Please sign in to comment.