Skip to main content
edited body
Source Link
Titian Cernicova-Dragomir
  • 252.9k
  • 37
  • 464
  • 394

This is a limitation in how control flow analysis works. The analysis does not cross function boundaries. You can read more here. The basic idea is that there is no guarantee prop.x will still not be undefined by the time the callback is called.

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

This is a limitation in how control flow analysis works. The analysis does not cross function boundaries. You can read more here

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

This is a limitation in how control flow analysis works. The analysis does not cross function boundaries. You can read more here. The basic idea is that there is no guarantee prop.x will still not be undefined by the time the callback is called.

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

edited body
Source Link
Titian Cernicova-Dragomir
  • 252.9k
  • 37
  • 464
  • 394

This is a limitation iNin how control flow analysis works. The analysis does not cross function boundaries. You can read more here

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

This is a limitation iN how control flow analysis works. The analysis does not cross function boundaries. You can read more here

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

This is a limitation in how control flow analysis works. The analysis does not cross function boundaries. You can read more here

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

edited body
Source Link
Titian Cernicova-Dragomir
  • 252.9k
  • 37
  • 464
  • 394

This is a limitation isiN how control flow analysis works. The analysis does not cross function boundaries. You can read more here

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

This is a limitation is how control flow analysis works. The analysis does not cross function boundaries. You can read more here

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

This is a limitation iN how control flow analysis works. The analysis does not cross function boundaries. You can read more here

The workaround is to put prop.x in a local variable. This will capture the flow type in the variable type:


const Example = (props: { x?: number, fn: (x: number) => void}) => {
        if (props.x !== undefined) {
            const x = props.x
            return <button onClick={() => props.fn(x)}>Click me</button>
        }
        return null;
}

Source Link
Titian Cernicova-Dragomir
  • 252.9k
  • 37
  • 464
  • 394
Loading