This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
type Foo<A> = { n: number, extra: A }; | |
type Foo2 = Foo<{s: string}>; | |
type Bar<A> = { foo: Foo<A> }; | |
type Bar2 = { foo: Foo2 }; | |
function getBarN(bar: Bar){ return bar.foo.n } | |
function getBarS(bar: Bar2){ return bar.foo.extra.s } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
type Foo = { n: number }; | |
type Foo2 = Foo & { s: string }; | |
type Bar = { foo: Foo }; | |
type Bar2 = { foo: Foo2 }; | |
function fooN(foo: Foo){ return foo.n; } | |
function barFooN(bar: Bar){ return bar.foo.n } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
// I want to write some shared logic with the 'core' data for an object: | |
type Widget = { id: number, customer_id: number } | |
function getCustomerId(w: Widget): number { | |
return w.customer_id | |
} | |
// But in another context Widgets have some extra information | |
type ManufacturableWidget = { id: number, customer_id: number, machine_id: number } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Union types work as subtypes | |
type Animal = { legs: number } | |
type Person = Animal & { name: string } | |
function numberOfLegs(a: Animal): number { | |
return a.legs; | |
} | |
const p: Person = { legs: 2, name: "Adam" } | |
numberofLegs(p) === 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import {Component} from 'react'; | |
export class Parent extends Component { | |
render () { | |
return <div> | |
<JustFunction foo={2}/> | |
<FullComponent foo={2}/> | |
</div> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I want to do something like this: | |
export default class Foo extends React.Component { | |
render() { | |
return <div ref={div => this._div = div}>I want a reference to this!</div> | |
} | |
} | |
// Flow says: "Property _div not found on Foo" | |
// Is there a way to give Foo the type Component & {_div?: Component} ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If I have a variant with two arms | |
type Foo = { type: "bar", bar: string } | { type: "baz", baz: string }; | |
// It would be nice if a function could handle those two arms. | |
function fromThingy(foo: Foo): string { | |
switch(foo.type) { | |
case "bar": | |
return foo.bar; | |
case "baz": | |
return foo.baz; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LESS version | |
var less = require("less"); | |
var lessTask = { | |
parseTime: function(source, options, callback) { | |
less.parse(source, options, function(err, root, imports, options) { | |
if (err) { return callback(err); } | |
var result; | |
try { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
function brieflyAddCss(cssCode) { | |
var styleElement = document.createElement("style"); | |
styleElement.type = "text/css"; | |
if (styleElement.styleSheet) { | |
styleElement.styleSheet.cssText = cssCode; | |
} else { | |
styleElement.appendChild(document.createTextNode(cssCode)); | |
} | |
document.head.appendChild(styleElement); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defprotocol ICursor | |
(value [self] "Get the current value of the cursor") | |
(transact [self f] "Update the current value of the cursor")) | |
(extend Atom | |
ICursor | |
(value [self] | |
(deref self)) | |
(transact [self f] | |
(swap! self f))) |