-
Notifications
You must be signed in to change notification settings - Fork 30.6k
feat(unist): add generic types for better type checking #54413
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
Merged
typescript-bot
merged 7 commits into
DefinitelyTyped:master
from
JounQin:feat/unist_generic
Jul 15, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a604570
feat(unist): add generic types for better type checking
JounQin 91a57ec
feat: mark Node#data as generic too
JounQin 6bd5e61
chore: add Data back
JounQin 859d5cb
test: add new test cases for infer
JounQin 8b08f76
refactor: clearer generic type names
JounQin ab9f7f2
docs: add comments
JounQin b6a5006
chore: use @link tag for reference
JounQin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,56 +1,148 @@ | ||
| import { Data, Point, Position, Node, Literal, Parent } from 'unist'; | ||
| import { Data, Point, Position, Node, Literal, Parent, NodeData } from 'unist'; | ||
|
|
||
| const data: Data = { | ||
| string: 'string', | ||
| number: 1, | ||
| object: { | ||
| key: 'value' | ||
| key: 'value', | ||
| }, | ||
| array: [], | ||
| boolean: true, | ||
| null: null | ||
| null: null, | ||
JounQin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| const point: Point = { | ||
| line: 1, | ||
| column: 1, | ||
| offset: 0 | ||
| offset: 0, | ||
| }; | ||
|
|
||
| const position: Position = { | ||
| start: point, | ||
| end: point, | ||
| indent: [1] | ||
| indent: [1], | ||
| }; | ||
|
|
||
| const node: Node = { | ||
| type: 'node', | ||
| data, | ||
| position | ||
| position, | ||
| }; | ||
|
|
||
| const text: Literal = { | ||
| type: 'text', | ||
| data, | ||
| position, | ||
| value: 'value' | ||
| value: 'value', | ||
| }; | ||
|
|
||
| const parent: Parent = { | ||
| type: 'parent', | ||
| data, | ||
| position, | ||
| children: [node, text] | ||
| children: [node, text], | ||
| }; | ||
|
|
||
| const noExtraKeysInNode: Node = { | ||
| type: 'noExtraKeysInNode', | ||
| // $ExpectError | ||
| extra: 'extra' | ||
| extra: 'extra', | ||
| }; | ||
|
|
||
| const noChildrenInNode: Node = { | ||
| type: 'noChildrenInNode', | ||
| // $ExpectError | ||
| children: [] | ||
| children: [], | ||
| }; | ||
|
|
||
| const stringLiteral: Literal<string> = { | ||
| type: 'text', | ||
| data, | ||
| position, | ||
| value: 'value', | ||
| }; | ||
|
|
||
| const literalParent: Parent<Literal<string>> = { | ||
| type: 'literalParent', | ||
| data, | ||
JounQin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| position, | ||
| children: [stringLiteral], | ||
| }; | ||
|
|
||
| const nodeData: Node<{ key: string }> = { | ||
| type: 'nodeData', | ||
| data: { | ||
| key: 'value', | ||
| }, | ||
| }; | ||
|
|
||
| const nodeData2: Node<{ key: string }> = { | ||
| type: 'nodeData', | ||
| // $ExpectError | ||
| data: {}, | ||
| }; | ||
|
|
||
| // $ExpectError | ||
| type DataType = NodeData<Node<string>>; | ||
|
|
||
| const literalData: Literal<string, { key: string }> = { | ||
| type: 'literalData', | ||
| data: { | ||
| key: 'value', | ||
| }, | ||
| value: 'value', | ||
| }; | ||
|
|
||
| const literalParentData: Parent<Literal<string>, Data> = { | ||
| type: 'literalParent', | ||
| data, | ||
| position, | ||
| children: [stringLiteral], | ||
| }; | ||
|
|
||
| const data1 = { | ||
| key1: 'value1', | ||
| }; | ||
|
|
||
| const data2 = { | ||
| key2: 'value2', | ||
| }; | ||
|
|
||
| const nestedliteralParentData: Parent<Literal<string, typeof data1>, typeof data2> = { | ||
| type: 'literalParent', | ||
| data: data2, | ||
| position, | ||
| children: [ | ||
| { | ||
| ...stringLiteral, | ||
| data: data1, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| function exampleNodeUtil(node: Node) {} | ||
|
|
||
| const inferredNode = { type: 'example' }; | ||
| const inferredNotNode = { notType: 'whoops' }; | ||
|
|
||
| exampleNodeUtil(inferredNode); | ||
| // $ExpectError | ||
| exampleNodeUtil(inferredNotNode); | ||
|
|
||
| function exampleLiteralUtil(node: Literal) {} | ||
|
|
||
| const inferredLiteral = { type: 'example', value: 'value' }; | ||
| const inferredNotLiteral = { type: 'example' }; | ||
|
|
||
| exampleLiteralUtil(inferredLiteral); | ||
| // $ExpectError | ||
| exampleLiteralUtil(inferredNotLiteral); | ||
|
|
||
| function exampleParentUtil(node: Parent) {} | ||
|
|
||
| const inferredParent = { type: 'example', children: [inferredNode] }; | ||
| const inferredNotParent = { type: 'example', children1: [] }; | ||
|
|
||
| exampleParentUtil(inferredParent); | ||
| // $ExpectError | ||
| exampleParentUtil(inferredNotParent); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.