Using flow-runtime to enforce strict type constraints at runtime

Name one relatively unknown project that everyone should be using? flow-runtime

Take Flow to the runtime journey.

Compilation-time type checking shortcomings

type WeatherResponseType = {|
+temperature: number
|};
const weatherResponseHead = await fetch('https://weather.api');const weatherResponse: WeatherResponseType = await weatherResponseHead.json();

Runtime type checking

{
"plugins": [
[
"flow-runtime",
{
"annotate": true,
"assert": true
}
]
]
}
import t from 'flow-runtime';const WeatherResponseType = t.type('WeatherResponseType', t.exactObject(t.property('temperature', t.number())));const weatherResponseHead = await fetch('https://weather.api');
const weatherResponse = WeatherResponseType.assert((await weatherResponseHead.json()));
{
"temperatureCelsius": 30,
"temperatureFahrenheit": 86
}
WeatherResponseType should not contain the key: "temperatureCelsius"Expected: {|
temperature: number;
|}
Actual: {
temperatureCelsius: number;
temperatureFahrenheit: number;
}
-------------------------------------------------WeatherResponseType should not contain the key: "temperatureFahrenheit"Expected: {|
temperature: number;
|}
Actual: {
temperatureCelsius: number;
temperatureFahrenheit: number;
}

Importance of catching the error early

{
"id": 1,
"name": "Foo"
}
type VenueType = {|
+id: number,
+name: string
|};
const venue: VenueType = await getVenueById(1);
const url = 'https://go2cinema.com/venues/' + venue.id + '-' + slugify(venue.name);
{
"name": "Foo",
"venueId": 1
}

The cost of runtime assertions

Weighting the benefits of runtime assertions

Author

Tech / Product Founder — building https://contra.com/