Skip to content

Instantly share code, notes, and snippets.

@AprilArcus
AprilArcus / MyError.ts
Created July 22, 2022 19:07
Pedantic JavaScript / TypeScript Error subclass
// Recipe for a pedantically correct subclass of JavaScript's built-in
// "Error" constructor.
// * Preserves the ability of JS Errors to be constructed with
// a `new`-less function call
// * Can be subclassed itself
// * Backward- and Forward-compatible TypeScript annotations
// * Correct enumerable / writable / configurable settings on
// .prototype, .prototype.constructor, .prototype.name, and
// custom instance fields.
/* @flow */
export function foo(test: boolean, callback: () => Array<number>): Array<number|string> {
if (test) return [1, '1'];
return callback();
}
3: export function foo(test: boolean, callback: () => Array<number>): Array<number|string> {
^^^^^^ string. This type is incompatible with
3: export function foo(test: boolean, callback: () => Array<number>): Array<number|string> {
$ flow check
src/injectCallbacks.js:0
0:
^ inconsistent use of library definitions
67: detachEvent?: (type: string, listener: EventListener) => void;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ undefined. This type is incompatible with. See: /private/tmp/flow/flowlib_2dc60f56/dom.js:67
67: detachEvent?: (type: string, listener: EventListener) => void;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See: /private/tmp/flow/flowlib_2dc60f56/dom.js:67
/* @flow */
declare class SVGElement extends Element {}
declare class HTMLParagraphElement extends HTMLElement {
align: string;
}
type ElementRegistrationOptions<T> = {
prototype?: {
@AprilArcus
AprilArcus / gist:7024adb7ec765f669e41
Last active January 7, 2016 03:17
custom element errors
element.js:
------------------------------
/* @flow */
declare class HTMLParagraphElement extends HTMLElement {
align: string
}
type CustomElementCallbacks = {
createdCallback?: () => void;
element.js
/* @flow */
declare class HTMLParagraphElement extends HTMLElement {
align: string
}
declare class Document extends Node {
registerElement(type: string, options?: ElementRegistrationOptions): void;
}
import { default as React, Component, Children, cloneElement } from 'react';
class ComponentWithComputedWidths extends Component {
constructor(props) {
super(props);
this.state = { computedWidthsAvailable = false; computedWidths: {} };
this.updateComputedWidths = this.updateComputedWidths.bind(this);
}
updateComputedWidths() {
<!-- polyfills -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/2.9.34/bluebird.min.js">
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.9.0/fetch.min.js">
<script>
var imageCache = {};
function preload(name) {
var url = "images/" + name + ".jpg";
return fetch(url) // returns a promise...
export default React.createClass({
displayName: 'Oceans Navigation Category',
propTypes: {
list: React.PropTypes.arrayOf(
React.PropTypes.shape({
humanReadableName: React.PropTypes.string,
url: React.PropTypes.string
})
// bootstrap React Router
import React from 'react';
import { default as Router, Route, DefaultRoute, HistoryLocation, RouteHandler } from 'react-router';
import { Splash } from '../components_angular/splash';
import index from './index';
let oceans;
if (!PRODUCTION) {
oceans = (
<Route handler={require('./oceans/navigation')} path='oceans'>