From 24cd52d6075966e9113524f002c0cb37133f1a37 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 9 Dec 2019 21:48:21 +0100 Subject: [PATCH] Add partial interface exports to types Closes GH-457. Reviewed-by: Christian Murphy Reviewed-by: Victor Felder Reviewed-by: Titus Wormer --- packages/remark-parse/types/index.d.ts | 6 ++++-- packages/remark-parse/types/test.ts | 15 +++++++++++++-- packages/remark-stringify/types/index.d.ts | 6 ++++-- packages/remark-stringify/types/test.ts | 15 ++++++++++++++- packages/remark/types/index.d.ts | 7 ++++--- packages/remark/types/test.ts | 11 +++++++++++ 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/packages/remark-parse/types/index.d.ts b/packages/remark-parse/types/index.d.ts index 9586f76a8..d61b070b5 100644 --- a/packages/remark-parse/types/index.d.ts +++ b/packages/remark-parse/types/index.d.ts @@ -13,8 +13,8 @@ declare class RemarkParser implements Parser { } declare namespace remarkParse { - interface Parse extends Plugin<[Partial?]> { - (options: Partial): void + interface Parse extends Plugin<[PartialRemarkParseOptions?]> { + (options: PartialRemarkParseOptions): void Parser: typeof RemarkParser } @@ -28,6 +28,8 @@ declare namespace remarkParse { pedantic: boolean } + type PartialRemarkParseOptions = Partial + interface Add { (node: Node, parent?: Parent): Node test(): Position diff --git a/packages/remark-parse/types/test.ts b/packages/remark-parse/types/test.ts index f600cc9d9..93a14ba39 100644 --- a/packages/remark-parse/types/test.ts +++ b/packages/remark-parse/types/test.ts @@ -1,13 +1,13 @@ 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' @@ -15,3 +15,14 @@ const badParseOptions = { // $ExpectError unified().use(remarkParse, badParseOptions) + +// $ExpectError +const parseOptions: remarkParse.RemarkParseOptions = { + gfm: true, + pedantic: true +} + +const badParseOptionsInterface: remarkParse.PartialRemarkParseOptions = { + // $ExpectError + gfm: 'true' +} diff --git a/packages/remark-stringify/types/index.d.ts b/packages/remark-stringify/types/index.d.ts index 20bcbb064..95f446fbe 100644 --- a/packages/remark-stringify/types/index.d.ts +++ b/packages/remark-stringify/types/index.d.ts @@ -11,9 +11,9 @@ declare class RemarkCompiler implements Compiler { } declare namespace remarkStringify { - interface Stringify extends Plugin<[Partial?]> { + interface Stringify extends Plugin<[PartialRemarkStringifyOptions?]> { Compiler: typeof RemarkCompiler - (this: Processor, options?: Partial): void + (this: Processor, options?: PartialRemarkStringifyOptions): void } type Compiler = RemarkCompiler @@ -40,6 +40,8 @@ declare namespace remarkStringify { emphasis: '_' | '*' } + type PartialRemarkStringifyOptions = Partial + type Visitor = (node: Node, parent?: Parent) => string } diff --git a/packages/remark-stringify/types/test.ts b/packages/remark-stringify/types/test.ts index 3908dd7cb..45b3e2d50 100644 --- a/packages/remark-stringify/types/test.ts +++ b/packages/remark-stringify/types/test.ts @@ -1,5 +1,6 @@ import remarkStringify = require('remark-stringify') import unified = require('unified') + import {Node, Parent} from 'unist' const inferredStringifyOptions = { @@ -12,7 +13,7 @@ unified().use(remarkStringify) unified().use(remarkStringify, inferredStringifyOptions) // These cannot be automatically inferred by TypeScript -const nonInferredStringifyOptions: Partial = { +const nonInferredStringifyOptions: remarkStringify.PartialRemarkStringifyOptions = { fence: '~', bullet: '+', listItemIndent: 'tab', @@ -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 +} diff --git a/packages/remark/types/index.d.ts b/packages/remark/types/index.d.ts index b14ba670c..70087245e 100644 --- a/packages/remark/types/index.d.ts +++ b/packages/remark/types/index.d.ts @@ -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 = Partial ->(): unified.Processor

+declare function remark(): unified.Processor export = remark diff --git a/packages/remark/types/test.ts b/packages/remark/types/test.ts index e4ef0e845..8b000913f 100644 --- a/packages/remark/types/test.ts +++ b/packages/remark/types/test.ts @@ -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' +}