Skip to content

Instantly share code, notes, and snippets.

@sandfox
Created January 19, 2018 16:01
Show Gist options
  • Select an option

  • Save sandfox/a4d8806ace1bdfbc54608133b6cf3ff6 to your computer and use it in GitHub Desktop.

Select an option

Save sandfox/a4d8806ace1bdfbc54608133b6cf3ff6 to your computer and use it in GitHub Desktop.
Flow type fun
//@flow
const {curry, get} = require('lodash/fp');
const safePropC = curry((propName, item) => item && item.props && item.props[propName]);
const safeProp = (propName, item) => item && item.props && item.props[propName];
const data = {
props: {
name: "james",
age: 20
}
}
// these should all pass fine
const age: number = safeProp('age', data)
const age_2: number = safePropC('age', data)
const age_3: number = get('props.age', data)
// The first one should fail and I'd expect flow to be unable
// to understand the tpye inference when lodash-ing
const x_age: number = safeProp('name', data)
const x_age_2: number = safePropC('name', data)
const x_age_3: number = get('props.name', data)
// when we run flow focus-checklol_wut_1.js
// It throw the correct error (sort of) but detects the error happening
// to a similair module
/*
Error: f.js:18
18: const age: number = safeProp('age', data)
^^^^^^^^^^^^^^^^^^^^^ string. This type is incompatible with
18: const age: number = safeProp('age', data)
^^^^^^ number
Error: f.js:23
23: const x_age: number = safeProp('name', data)
^^^^^^^^^^^^^^^^^^^^^^ string. This type is incompatible with
23: const x_age: number = safeProp('name', data)
^^^^^^ number
Found 2 errors
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment