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

Flow not able to automatically detect type from HTMLNode dataset #3997

Closed
jigarjain opened this Issue May 22, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@jigarjain

jigarjain commented May 22, 2017

Hello there,

I am not sure whether this is a bug or rather me using the Flow incorrectly. But the problem is this - I have a simple function which takes a HTML Node as input as returns a certain data-{*} value from the sibling of that node. The function is as below:

function getSiblingDataName(node: Node, direction: string): ?string {
    const startNode = node;
    let name = null;

    // startNode is a correct node only if it has dataset.name
    if (startNode && startNode.dataset && startNode.dataset.name) {
        const sibling = direction === 'previous' ? startNode.previousSibling : startNode.nextSibling;

        if (sibling && sibling.dataset && sibling.dataset.name) {
            name = sibling.dataset.name;
        }
    }

    return name;
}

Now I am assuming that flow should be able to interpret dataset which is a standard property on the HTMLElement/Node & accordingly know that name can either be null or string only.

But on running flow check I get the error as below:

131:     return name;
                ^^^^^^ property `name` of unknown type. This type is incompatible with the expected return type of
113: function getSiblingDataName(node: Node, direction: string): ?string {
                                                                 ^^^^^^ string

I can solve this issue my typecasting as below but I really want to avoid doing that since I always know that if sibling.dataset.name is set, then it will always be string:

name = String(sibling.dataset.name);

Is this the only correct way to do it? It seems to me that since my input is defined to be of type Node, flow can expect to have dataset property on it.

@agentcooper

This comment has been minimized.

Show comment
Hide comment
@agentcooper

agentcooper May 22, 2017

Contributor

dataset is defined only on HTMLElement.

I rewrote your code to use HTMLElement instead of Node (notice nextElementSibling vs nextSibling):

function getSiblingDataName(element: HTMLElement, direction: string): ?string {
    const startElement = element;
    let name = null;

    // startNode is a correct node only if it has dataset.name
    if (startElement && startElement.dataset && startElement.dataset.name) {
        const sibling =
              direction === 'previous'
              	? startElement.previousElementSibling
        	: startElement.nextElementSibling;

        if (sibling instanceof HTMLElement && sibling.dataset && sibling.dataset.name) {
            name = sibling.dataset.name;
        }
    }

    return name;
}

Try

Contributor

agentcooper commented May 22, 2017

dataset is defined only on HTMLElement.

I rewrote your code to use HTMLElement instead of Node (notice nextElementSibling vs nextSibling):

function getSiblingDataName(element: HTMLElement, direction: string): ?string {
    const startElement = element;
    let name = null;

    // startNode is a correct node only if it has dataset.name
    if (startElement && startElement.dataset && startElement.dataset.name) {
        const sibling =
              direction === 'previous'
              	? startElement.previousElementSibling
        	: startElement.nextElementSibling;

        if (sibling instanceof HTMLElement && sibling.dataset && sibling.dataset.name) {
            name = sibling.dataset.name;
        }
    }

    return name;
}

Try

@jigarjain

This comment has been minimized.

Show comment
Hide comment
@jigarjain

jigarjain commented May 24, 2017

@agentcooper - Thanks

@jigarjain jigarjain closed this May 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment