Skip to content

Instantly share code, notes, and snippets.

@tlrobinson
Created September 28, 2016 21:38
Show Gist options
  • Save tlrobinson/c5439428c2c8880fd8f1b669b242ec18 to your computer and use it in GitHub Desktop.
Save tlrobinson/c5439428c2c8880fd8f1b669b242ec18 to your computer and use it in GitHub Desktop.
Flow typechecked getIn (icepick, immutable, etc)
type K = string | number;
type getIn0 = <V>(o: V, p: [void]) => V
type getIn1 = <K1:K,V>(o: {[k:K1]:V}, p: [K1,void]) => V
type getIn2 = <K1:K,K2:K,V>(o: {[k:K1]:{[k:K2]:V}}, p: [K1,K2,void]) => V
type getIn3 = <K1:K,K2:K,K3:K,V>(o: {[k:K1]:{[k:K2]:{[k:K3]:V}}}, p: [K1,K2,K3,void]) => V
type getIn4 = <K1:K,K2:K,K3:K,K4:K,V>(o: {[k:K1]:{[k:K2]:{[k:K3]:{[k:K4]:V}}}}, p: [K1,K2,K3,K4,void]) => V
type getIn5 = <K1:K,K2:K,K3:K,K4:K,K5:K,V>(o: {[k:K1]:{[k:K2]:{[k:K4]:{[k:K5]:V}}}}, p: [K1,K2,K3,K4,K5,void]) => V
type getInN = <V>(o: Object, p: [K,K,K,K,K,K]) => V;
type getInType = getInN & getIn5 & getIn4 & getIn3 & getIn2 & getIn1 & getIn0
declare var getIn: getInType
let object = { foo: { bar: { baz: { buz: { boz: 1234 } } } } };
// should not typecheck
getIn(object, []).foo.bar.baz.buz.boz
getIn(object, ["foo"]).bar.baz.buz.boz
getIn(object, ["foo", "bar"]).baz.buz.boz
getIn(object, ["foo", "bar", "baz"]).buz.boz
getIn(object, ["foo", "bar", "baz", "buz"]).boz
getIn(object, ([]: Array<string>)).boz;
// should typecheck:
getIn(object, []).nope
getIn(object, ["foo"]).nope
getIn(object, ["foo", "bar"]).nope
getIn(object, ["foo", "bar", "baz"]).nope
getIn(object, ["foo", "bar", "baz", "buz"]).nope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment