diff --git a/types/vfile/index.d.ts b/types/vfile/index.d.ts index 2ef3b03f19..792644dacc 100644 --- a/types/vfile/index.d.ts +++ b/types/vfile/index.d.ts @@ -10,26 +10,25 @@ import * as Unist from 'unist'; declare namespace vfile { - type Contents = string | Buffer; + type VFileContents = string | Buffer; interface NodeWithPosition extends Unist.Node { position: Unist.Position; [key: string]: any; } - interface VFileParamsBase { - data?: D; - contents?: Contents; + interface VFileOptions { + contents?: VFileContents; path?: string; basename?: string; stem?: string; extname?: string; dirname?: string; cwd?: string; + data?: any; + [key: string]: any; } - type VFileParams = VFileParamsBase & C; - /** * File-related message describing something at certain position. */ @@ -102,12 +101,11 @@ declare namespace vfile { */ type ToString = (encoding?: BufferEncoding) => string; - interface VFileBase { + interface VFile { /** * @param options If `options` is `string` or `Buffer`, treats it as `{contents: options}`. If `options` is a `VFile`, returns it. All other options are set on the newly created `vfile`. */ - (input?: Contents): VFile; - (input?: VFile | VFileParams): VFile; + (input?: VFileContents | F | VFileOptions): F; message: Message; fail: Fail; info: Info; @@ -119,7 +117,7 @@ declare namespace vfile { * Place to store custom information. * It's OK to store custom data directly on the `vfile`, moving it to `data` gives a little more privacy. */ - data: C['data']; + data: any; /** * List of messages associated with the file. */ @@ -127,7 +125,7 @@ declare namespace vfile { /** * Raw value. */ - contents: Contents; + contents: VFileContents; /** * Path of `vfile`. * Cannot be nullified. @@ -161,8 +159,6 @@ declare namespace vfile { cwd: string; toString: ToString; } - - type VFile = VFileBase & C; } /** @@ -170,6 +166,6 @@ declare namespace vfile { * Path related properties are set in the following order (least specific to most specific): `history`, `path`, `basename`, `stem`, `extname`, `dirname`. * It’s not possible to set either `dirname` or `extname` without setting either `history`, `path`, `basename`, or `stem` as well. */ -declare const vfile: vfile.VFile<{}>; +declare const vfile: vfile.VFile; export = vfile; diff --git a/types/vfile/vfile-tests.ts b/types/vfile/vfile-tests.ts index 0b19246ae8..4d146237b1 100644 --- a/types/vfile/vfile-tests.ts +++ b/types/vfile/vfile-tests.ts @@ -1,4 +1,4 @@ -import vfile = require("vfile"); +import vfile = require('vfile'); import * as Unist from 'unist'; // Instantiation @@ -7,21 +7,30 @@ vfile('string'); vfile(Buffer.from('string')); vfile(vfile()); vfile({ stem: 'readme', extname: '.md' }); -vfile({ custom: 'data' }); try { vfile({ extname: '.md' }); } catch (e) { console.log('Error: set extname without path'); } -const file = vfile<{custom: string, data: {custom: number}}>({ +interface CustomVFile extends vfile.VFile { + custom: string; + data: { + custom: number; + }; +} + +const file = vfile({ path: '~/example.txt', contents: 'Alpha *braavo* charlie.', custom: 'Custom tango', data: { custom: 12345 - }, + } }); +const customIsString: string = file.custom; +const dataCustomIsNumber: number = file.data.custom; +const copiedFile: CustomVFile = vfile(file); file.path; // => '~/example.txt' file.dirname; // => '~'