Meetup, November 13th, 2018


type MapToPromise<T> = { [K in keyof T]: Promise<T[K]> };
type Coordinate = [number, number]
type PromiseCoordinate = MapToPromise<Coordinate>; // [Promise<number>, Promise<number>]
function readImage(path: string, callback: (err: any, image: Image) => void) {
// ...
}
readImage.sync = (path: string) => {
const contents = fs.readFileSync(path);
return decodeImageSync(contents);
}
let f00 = foo.bind(undefined); // (a: number, b: string) => string
let f01 = foo.bind(undefined, 10); // (b: string) => string
let f02 = foo.bind(undefined, 10, "hello"); // () => string
type Result<T> = { error?: undefined, value: T } | { error: Error };
function test(x: Result<number>) {
if (!x.error) {
x.value; // number (used to be error)
}
else {
x.error.message; // string
}
}
test({ value: 10 });
test({ error: new Error("boom") });
function taggedObject<T, U extends string>(obj: T, tag: U) {
return { ...obj, tag }; // T & { tag: U }
}
let x = taggedObject({ x: 10, y: 20 }, "point"); // { x: number, y: number } & { tag: "point" }
Non-generic spreads are merged as much as possible
function foo1<T>(t: T, obj1: { a: string }, obj2: { b: string }) {
// { a: string, x: number } & T & { b: string, y: number }
return { ...obj1, x: 1, ...t, ...obj2, y: 2 };
}
Non-generic spread expressions continue to be processed as before
(!) This differs from intersection types
function excludeTag<T extends { tag: string }>(obj: T) {
let { tag, ...rest } = obj;
return rest; // Pick<T, Exclude<keyof T, "tag">>
}
const taggedPoint = { x: 10, y: 20, tag: "point" };
const point = excludeTag(taggedPoint); // { x: number, y: number }
class Foo<T, U> {
constructor(value1: T, value2: U) { }
}
const instance = new Foo<_, string>(0, "");
The real question is should we use dog or cat gifs for our comments?