Skip to content

Fix type assertion #3

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
merged 5 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* @typedef {null|undefined|TagName|TestFunctionAnything|Array.<TagName|TestFunctionAnything>} Test
*/

/**
* @template {Element} T
* @typedef {null|undefined|T['tagName']|TestFunctionPredicate<T>|Array.<T['tagName']|TestFunctionPredicate<T>>} PredicateTest
*/

/**
* Check if an element passes a test
*
Expand Down Expand Up @@ -56,8 +61,10 @@ export const isElement =
* When a `parent` node is known the `index` of node should also be given.
*
* @type {(
* (<T extends Element>(node: unknown, test: T['tagName']|TestFunctionPredicate<T>|Array.<T['tagName']|TestFunctionPredicate<T>>, index?: number, parent?: Parent, context?: unknown) => node is T) &
* ((node?: unknown, test?: Test, index?: number, parent?: Parent, context?: unknown) => boolean)
* (() => false) &
Copy link
Member Author

Choose a reason for hiding this comment

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

I still think we should remove this in typings.

Copy link
Member

Choose a reason for hiding this comment

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

What is the benefit in removing it?
The benefit in having it is that it matches how the code works. Generally, types should match the code. Why deviate from the code and introduce a breaking change?

Copy link
Member Author

@JounQin JounQin Dec 1, 2021

Choose a reason for hiding this comment

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

Generally, types should match the code.

TypeScript should be stricter than codes, the benefit is that isElement() is always meaningless in practice, and you should not use it at all. For example, parseInt(num) or parseInt() are valid in js, but error in ts.

introduce a breaking change

Personally, I won't say updating better practice types as breaking change.

* (<T extends Element>(node: unknown, test: PredicateTest<T>, index?: number, parent?: Parent, context?: unknown) => node is T) &
* ((node: unknown, test?: PredicateTest<Element>, index?: number, parent?: Parent, context?: unknown) => node is Element) &
* ((node: unknown, test: Test, index?: number, parent?: Parent, context?: unknown) => boolean)
* )}
*/
(
Expand Down
31 changes: 30 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const article: Element = {
const isSection = (element: Element): element is Section =>
element.tagName === 'section'

isElement()
expectType<false>(isElement())

/* Missing parameters. */
expectError(isElement<Section>())
Expand Down Expand Up @@ -133,3 +133,32 @@ convertElement()
convertElement(null)
convertElement(undefined)
expectError(convertElement<Article>())

declare const node: unknown

/* Type assertion */
if (isElement(node)) {
expectType<Element>(node)
}

if (
isElement<Section>(
node,
(node): node is Section => 'tagName' in node && node.tagName === 'section'
)
) {
expectType<Section>(node)
}

if (isElement(node, node => 'children' in node)) {
expectType<unknown>(node)
} else {
expectType<unknown>(node)
}


if (isElement(node) && 'children' in node) {
expectType<Element>(node)
} else {
expectType<unknown>(node)
}