Skip to content

Instantly share code, notes, and snippets.

@tlareg
Last active May 5, 2022 15:51
Show Gist options
  • Save tlareg/d47a9449096bde751edf069deae970f6 to your computer and use it in GitHub Desktop.
Save tlareg/d47a9449096bde751edf069deae970f6 to your computer and use it in GitHub Desktop.
typescript cheatsheet
type CallbackFunction = () => void;
type CallbackFunctionVariadic = (...args: any[]) => void;
type CallbackFunctionVariadicAnyReturn = (...args: any[]) => any;
type CallbackFunctionSomeVariadic = (arg1: string, arg2: number, ...args: any[]) => void;
// MIXIN
type Constructor<T> = new(...args: any[]) => T;
interface Tagged {
_tag: string;
}
function Tagged<T extends Constructor<{}>>(Base: T): Constructor<Tagged> & T {
return class extends Base {
_tag: string;
constructor(...args: any[]) {
super(...args);
this._tag = "";
}
}
}
const TaggedPoint: Constructor<Tagged> & typeof Point = Tagged(Point);
let point: Tagged & Point = new TaggedPoint(10, 20);
point._tag = "hello";
interface Jakis { dupa: number }
function a<T extends Jakis>(x: T) {}
a({dupa: 8, cycki: 9})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment