Skip to content

Instantly share code, notes, and snippets.

@jonakyd
Created March 24, 2019 23:34
Show Gist options
  • Save jonakyd/ac000b3fe30161d2b185083756b57176 to your computer and use it in GitHub Desktop.
Save jonakyd/ac000b3fe30161d2b185083756b57176 to your computer and use it in GitHub Desktop.
Flow quirk
function a(args: { a1: Array<number>, a2: number }) {
// Should Error
console.log(args.a1.join(','));
}
a({});
// Ok, we try the exact object now
function b(args: {| a1: Array<number>, a2: number |}) {
console.log(args.a1.join(','));
}
// $ExpectError
// Good
// b({});
b({a1: [1,2,3], a2: 1});
// What if we want optional properties?
function c(args: {| a1?: Array<number>, a2?: number |}) {
if (args.a1) {
console.log(args.a1.join(','));
}
}
// Why it's error?
//c({});
// Still error WTF
//c({a1: undefined, a2:undefined});
const sealedObject = {
a1: undefined,
a2: undefined,
};
// Flow is ok now. But this way doesnt feel right...
c(sealedObject);
@jonakyd
Copy link
Author

jonakyd commented Mar 25, 2019

// What if we want optional properties?
type Args = {| a1: Array<number>, a2: number |} & { b1?: Array<number>, b2?: number };

function c(args?: Args) {
  if (args && args.a1) {
    console.log(args.a1.join(','));
  }
}

// Error, Whyyyyyyyyyyy
//c({});

c()

Required props and optional hack with &

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