diff --git a/types/vfile/readme.md b/types/vfile/readme.md index 349503cef8..21525242cb 100644 --- a/types/vfile/readme.md +++ b/types/vfile/readme.md @@ -3,6 +3,34 @@ Below are some examples of how to use these Definitions.
Scenarios where the Typescript code is identical to plain Javascript code are omitted. +### Using `data` property + +Because `data` property type is set `unknown`, you have to make your own type guard. + +```ts +interface CustomData { + message: string; +} +// This is the type guard +function isCustomData(data: any): data is CustomData { + return data != null && typeof data.message === 'string'; +} +function doSomethingWithCustomData(data: CustomData) { +} +const customData: CustomData = { + message: 'tango' +}; +const fileWithData = vfile({ + data: customData +}); + +if (isCustomData(fileWithData.data)) { + doSomethingWithCustomData(fileWithData.data); +} +// The below code will throw a compiler error +doSomethingWithCustomData(fileWithData.data); // $ExpectError +``` + ### Extending VFile ```ts