flow icon indicating copy to clipboard operation
flow copied to clipboard

Flow prop validation for function doesn't verify signature/parameter list

Open kvkirthy opened this issue 9 years ago • 7 comments

Consider following type for Props

export type CounterProps = {
    offset: number;
    eventCallback: (num: number, eventTime: Date) => void;
};

Callback signature is not verified while invoking the component. Flow shows no errors.

// Use Counter as below and Flow finds no errors
// Notice eventCallback was not provided with second parameter (eventTime).
 <Counter offset={4} eventCallback = {(num: number) => console.log(`At Counter Value is ${num} `)} />

// Same problem with default props too
Counter.defaultProps = {offset: 1, eventCallback: (num: number) => {}};

Component definition as below,

export default class Counter extends React.Component{
    props: CounterProps;
    static defaultProps: CounterProps;

    constructor(props: CounterProps){
        super(props);
    }

// rest of class definition

}

kvkirthy avatar Dec 16 '16 10:12 kvkirthy

This is not a bug. A function with less arguments is considered a subtype.

vkurchatkin avatar Dec 16 '16 10:12 vkurchatkin

@vkurchatkin It doesn't work (show error) for more number of parameters or change in data type.

Following doesn't return an error

// notice num is string. Type alias has it a number
eventCallback = {(num: string, eventTime: Date) => console.log(`At Counter Value is ${num} `)} 

// more params
<Counter offset={4} eventCallback = {(num: number, eventTime: Date, val: string) => console.log(`At Counter Value is ${num} `)} />

Where as documentation at this link says

While PropTypes are great, they are quite limited. For example, it’s possible to specify that a prop is some kind of function, but not what parameters that function accepts or what kind of value it might return.

Flow has a much richer type system which is able to express those constraints and more. With class-based components, you can specify the components’ props, default props, and state using Flow’s annotation syntax.

kvkirthy avatar Dec 16 '16 11:12 kvkirthy

@kvkirthy can you post a complete example on https://flowtype.org/try/ ? I tried it and it works fine

vkurchatkin avatar Dec 16 '16 12:12 vkurchatkin

@vkurchatkin Tried to share it with you and it works on flowtype.org/try

However, on a Windows 7 machine it still doesn't. Please review screenshot at this link

kvkirthy avatar Dec 16 '16 13:12 kvkirthy

@vkurchatkin It didn't work on bare bone command prompt either. Screenshot

kvkirthy avatar Dec 16 '16 13:12 kvkirthy

Can you still reproduce this problem? The code needs tiny syntax changes because of Flow v0.53, but it seems to work as expected (giving type errors that it should) both on Try Flow and on my desktop.

(If I had to guess, and if this is Windows-specific, I would say maybe the flow server has a Windows-specific bug where it caches old file contents or something? Does it work if you make a new file and paste that in? Or if you add a new, simpler type error to the file, does that show up?)

(This is maybe closable if it's already fixed, or else should be labeled a Windows-specific bug. /cc @calebmer)

asolove avatar Aug 22 '17 03:08 asolove

What version of Flow are you on? This should work in Flow v0.53. I’m going to close for now, but if you continue to get this error in Flow v0.53 please re-open! I wouldn’t expect this code path to be significantly different on Windows 😣

calebmer avatar Aug 27 '17 20:08 calebmer