Skip to content

Instantly share code, notes, and snippets.

@romellem
Created November 25, 2022 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romellem/4f407b1c4b76e5ea1175ad03d29e23e1 to your computer and use it in GitHub Desktop.
Save romellem/4f407b1c4b76e5ea1175ad03d29e23e1 to your computer and use it in GitHub Desktop.
Flow argument variance with object's enum value
// @flow
// https://flow.org/try/#0PTAEAEDMBsHsHcBQiAuBPADgU1AUQHYCuAtgIIDGKAlrPqALygDkAhk6AD7MBG7XT5JgG5k6bKACS+AG4sATlRb4UFarQagA3h0ShQxLCgAWsACYAuPETKUa+ADSIOAXxGpMOAMKxZCpStt1Rm1dUABqA2MzSwISVTtHFzdIQnxAulNYKV9FZQAVIyp8AHMAChZ0y2z5XIC1fABKLWdEFLT60EzvHP8CorKK+stumv942ibNFtEPUDyPAHluACsNbX1DEwtmNk5XRHJaAGcUUGN+y3nsJdXgjajt1nZ9xEzqv3zCktLzkoaRLo+Uaffo-L7Ff6IIA
type EnumAction = 'a' | 'b' | 'c';
type InvariantAction = {|
method: EnumAction,
|};
type CovariantAction = {|
+method: EnumAction,
|};
function doInvariantThing(action: InvariantAction) {}
function doCovariantThing(action: CovariantAction) {}
type TypeObj = {| method: 'a' |};
const thing: TypeObj = { method: 'a' };
doInvariantThing(thing);
doCovariantThing(thing);
// 20: doInvariantThing(thing);
// ^ Cannot call `doInvariantThing` with `thing` bound to `action` because string literal `a` [1] is incompatible with string literal `b` [2] in property `method`. [incompatible-call]
// References:
// 17: type TypeObj = {| method: 'a' |};
// ^ [1]
// 6: method: EnumAction,
// ^ [2]
// 20: doInvariantThing(thing);
// ^ Cannot call `doInvariantThing` with `thing` bound to `action` because string literal `a` [1] is incompatible with string literal `c` [2] in property `method`. [incompatible-call]
// References:
// 17: type TypeObj = {| method: 'a' |};
// ^ [1]
// 6: method: EnumAction,
// ^ [2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment