codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Support for optional arguments and optional properties of object types #19

Closed motiz88 closed 9 years ago

motiz88 commented 9 years ago

The added functionality and tests reflect my understanding of Flow's optional properties and optional arguments semantics.

Optional arguments use the x?: T syntax that the Flow docs mention in passing.

phpnode commented 9 years ago

awesome, thanks!

It's sort of weird that there are so many ways to do the same thing with flow syntax, for function arguments I've been using:

function foo (str: ? string) {}

But I agree that

function foo (str?: string) {}

is much nicer, I wonder why it allows both.

motiz88 commented 9 years ago

Those are actually somewhat different. ?T is a nullable or so-called maybe type - effectively shorthand for T | null | undefined, whereas x?: T is narrower to the effect of x: T | undefined, omitting the null.

This is what I was referring to as "my understanding of Flow's ... semantics", which incidentally I'm now more confident about, having read this. Some of the test cases I wrote verify this exact distinction (passing null in an optional param should fail, likewise in an optional property of an object-typed param).