Fix abuse of generic type

This commit is contained in:
Junyoung Choi
2018-11-26 15:43:36 +09:00
parent 36a32d8a72
commit bcd33769d5
2 changed files with 23 additions and 18 deletions
+10 -14
View File
@@ -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<D> {
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<C extends {data?: {}}> = VFileParamsBase<C['data']> & C;
/**
* File-related message describing something at certain position.
*/
@@ -102,12 +101,11 @@ declare namespace vfile {
*/
type ToString = (encoding?: BufferEncoding) => string;
interface VFileBase<C extends {data?: {}}> {
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<C>;
<C>(input?: VFile<C> | VFileParams<C>): VFile<C>;
<F extends 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<C> = VFileBase<C> & 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;
+13 -4
View File
@@ -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<CustomVFile>({
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; // => '~'