Skip to content

Instantly share code, notes, and snippets.

View danvk's full-sized avatar

Dan Vanderkam danvk

View GitHub Profile
const order = {
  what: 'Macbook Air' as Product, // or "as 'MacbookAir'"
  how: 'bitcoin'
};
purchase(order.what); // ok
const order = {
 what: 'Macbook Air',
 how: 'bitcoin'
};
order.what = 'Lenovo'; // valid
let newToy: Product = 'Macbook Air';
purchase(newToy); // ok
purchase('Macbook Air');
const newToy = 'Macbook Air';
purchase(newToy); // ok
type Product = 'iPad' | 'Mac Mini' | 'Macbook Air' | 'Macbook Pro';
export function purchase(what: Product) {
 // mine bitcoin…
 // send to apple…
}
const loc: [number, number] = [10, 20];
panTo(loc);
const loc: any = [10, 20];
panTo(loc);
const loc = [10, 20];
panTo(loc as any);
@danvk
danvk / valid.js
Last active January 22, 2019 20:11
const loc = [10, 20];
loc.push(30);
// Parameter is a (latitude, longitude) pair.
function panTo(where: [number, number]) { /* ... */ }
// Inline:
panTo([10, 20]); // inline: ok
// Reference:
const loc = [10, 20];
panTo(loc); // reference. Will this work?
export function purchase(what: string) { /* ... */ }
purchase('Macbook Air'); // Inline
const product = 'Macbook Air';
purchase(product); // reference