mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-05 15:27:05 +00:00
Exploit generics to assert typings of custom data
This commit is contained in:
Vendored
+10
-5
@@ -24,7 +24,8 @@ declare namespace vfile {
|
||||
position?: Position;
|
||||
}
|
||||
|
||||
interface VFileParams {
|
||||
interface VFileParamsBase<D> {
|
||||
data?: D;
|
||||
contents?: Contents;
|
||||
path?: string;
|
||||
basename?: string;
|
||||
@@ -34,6 +35,8 @@ declare namespace vfile {
|
||||
cwd?: string;
|
||||
}
|
||||
|
||||
type VFileParams<C, D> = VFileParamsBase<D> & C;
|
||||
|
||||
interface VFileError {
|
||||
ruleId: string | null;
|
||||
name: string;
|
||||
@@ -56,13 +59,13 @@ declare namespace vfile {
|
||||
|
||||
type ToString = (encoding?: BufferEncoding) => string;
|
||||
|
||||
interface VFile {
|
||||
(input?: Contents | VFile | VFileParams): VFile;
|
||||
interface VFileBase<C, D> {
|
||||
<C>(input?: Contents | VFile<C, D> | VFileParams<C, D>): VFile<C, D>;
|
||||
message: Message;
|
||||
fail: Fail;
|
||||
info: Info;
|
||||
history: string[];
|
||||
data: {};
|
||||
data: D;
|
||||
messages: VFileError[];
|
||||
contents: Contents;
|
||||
path?: string;
|
||||
@@ -73,8 +76,10 @@ declare namespace vfile {
|
||||
cwd: string;
|
||||
toString: ToString;
|
||||
}
|
||||
|
||||
type VFile<C, D> = VFileBase<C, D> & C;
|
||||
}
|
||||
|
||||
declare const vfile: vfile.VFile;
|
||||
declare const vfile: vfile.VFile<{}, {}>;
|
||||
|
||||
export = vfile;
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import vfile = require("vfile");
|
||||
|
||||
const file = vfile({path: '~/example.txt', contents: 'Alpha *braavo* charlie.'});
|
||||
const file = vfile({
|
||||
path: '~/example.txt',
|
||||
contents: 'Alpha *braavo* charlie.',
|
||||
custom: 'Custom tango',
|
||||
data: {
|
||||
custom: 12345
|
||||
}
|
||||
});
|
||||
|
||||
file.path; // => '~/example.txt'
|
||||
file.dirname; // => '~'
|
||||
@@ -16,3 +23,7 @@ file.history; // => ['~/example.txt', '~/example.md', '~/index.text']
|
||||
file.message('`braavo` is misspelt; did you mean `bravo`?', {line: 1, column: 8});
|
||||
|
||||
console.log(file.messages);
|
||||
|
||||
// Keep typings of custom data
|
||||
const custom: string = file.custom; // 'Custom tango'
|
||||
const dataCustom: number = file.data.custom; // 12345
|
||||
|
||||
Reference in New Issue
Block a user