From 07bc4dae5dfa800aa29a5abfc864a2a6447c58a2 Mon Sep 17 00:00:00 2001 From: "Kevin @ Navigram" <37534869+Kevin-at-Navigram@users.noreply.github.com> Date: Fri, 5 Apr 2019 14:41:10 +0200 Subject: [PATCH] Added TypeScript basics Added TypeScript basics based on issue #64 conversation. --- README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 691ac82..bcc4954 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ You can maually find the definition files in the `types` folder. If you have any doubt using this library please post a question on [stackoverflow](http://stackoverflow.com/questions/ask?tags=treemodel) tagged with `treemodel`. -## API Reference +## JavaScript API Reference ### Create a new TreeModel @@ -159,6 +159,26 @@ node.walk([options], action, [context]) These functions can also take, as the last parameter, the *context* on which the action will be called. +## Typescript Refrence + +You can maually find the definition files in the `types` folder. A quick summary, is rougly as follows: + +```ts +import * as TreeModel from "tree-model"; + +interface TestModel { + name: string; +} +const tree = new TreeModel({}); +const root = tree.parse({ name: 'a', children: [{ name: 'b' }, { name: 'c' }] }); +``` +It is also possible to type the input object (to parse) itself, instead of providing the generic type to `.parse`: + +```ts +const data : TreeModel.Model = { name: 'a', children: [{ name: 'b' }, { name: 'c' }] }; +const root = tree.parse(data); +``` + ## Contributing ### Setup