Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TypeScript basics #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets call this TypeScript Example because it's not really a reference

Suggested change
## Typescript Refrence
## TypeScript Example


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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like a more complete example that also type checks the children, maybe:

Suggested change
name: string;
name: string,
children?: Array<TestModel>

But I don't know enough TypeScript...

}
const tree = new TreeModel({});
const root = tree.parse<TestModel>({ 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<TestModel> = { name: 'a', children: [{ name: 'b' }, { name: 'c' }] };
const root = tree.parse(data);
```

## Contributing

### Setup
Expand Down