From dcf5a0dbd497d343a92b6084440c7ffb12cc220f Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 27 Nov 2018 14:47:42 +0900 Subject: [PATCH] Add decription for `data` prop --- types/vfile/readme.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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