Add Typescript type definitions for most of Racer's Model API #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds a TypeScript type definition file that covers most of Racer's public Model API. These require TypeScript >= 3.4.
To test these definitions out against this PR's branch:
For usage examples, see the new tests:
https://github.com/lever/racer/blob/6133d0cf7617b5669829ef9f903891a6dfb5ff0b/test/types-test.ts
Supported and unsupported features
Supported features in this initial set of type definitions:
These features are not included in these initial type definitions:
Model#start
Model#ref
Model#on
Model#filter
Implementation notes
Static typing of model data:
Model<T>
has a generic type parameter that should be the type of the data stored at that model's path. Unfortunately, getter and mutator methods don't have data-based type safety, since subpaths are passed in as dot-separated strings.ReadonlyDeep
:model.data
. Such return values should not be directly modified, so these type definitions returnReadonlyDeep
versions of types where appropriate, which disallow object property assignments and Array mutations.Model#getDeepCopy
for returning a recursively deep-copied value, or you can manually do shallow copies where appropriate, e.g. viaObject.assign({}, value)
for an object orarray.slice()
for an array.