Skip to content

Instantly share code, notes, and snippets.

View tswistak's full-sized avatar

Tomasz Świstak tswistak

View GitHub Profile
@tswistak
tswistak / lib.es5.ts
Created January 16, 2019 10:33
Avoiding any in TypeScript, listing 6
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
@tswistak
tswistak / partial.ts
Created January 16, 2019 10:30
Avoiding any in TypeScript, listing 5
class Settings {
setting1: boolean = true;
setting2: string = '';
setting3: number = 1;
constructor(obj: Partial<Settings>) {
Object.assign(this, obj);
}
}
const obj = new Settings({ setting3: 2 });
@tswistak
tswistak / lib.es5.ts
Created January 16, 2019 10:27
Avoiding any in TypeScript, listing 4
type Partial<T> = {
[P in keyof T]?: T[P];
};
type Required<T> = {
[P in keyof T]-?: T[P];
};
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
@tswistak
tswistak / record.ts
Created January 16, 2019 10:26
Avoiding any in TypeScript, listing 3
const dict2: { a?: string } & Record<string, number> = {};
dict2.a = 'a';
dict2.a = 1; // ERROR: 1 is not assignable to type string
dict2.b = 2;
dict2.b = 'b'; // ERROR: "b" is not assignable to type number
@tswistak
tswistak / record.ts
Created January 16, 2019 10:24
Avoiding any in TypeScript, listing 2
const dict: Record<string, number> = {};
dict.a = 1;
dict.b = 'a'; // ERROR: "a" is not assignable to type number
@tswistak
tswistak / lib.es5.ts
Created January 16, 2019 10:20
Avoiding any in TypeScript, listing 1
type Record<K extends keyof any, T> = {
[P in K]: T;
};
@tswistak
tswistak / Hello.tsx
Created August 25, 2018 15:42
InversifyJS with React, listing 4
import * as React from "react";
import { inject, injectable } from "inversify";
import { connect } from "react-inversify";
import { IProvider } from "./providers";
type Props = {
nameProvider: IProvider<string>;
};
@injectable()
@tswistak
tswistak / Hello.tsx
Created August 25, 2018 13:54
InversifyJS with React, listing 3
import * as React from "react";
import { resolve } from "inversify-react";
import { IProvider } from "./providers";
export class Hello extends React.Component {
@resolve("nameProvider") private readonly nameProvider: IProvider<string>;
render() {
return <h1>Hello {this.nameProvider.provide()}!</h1>;
}
@tswistak
tswistak / Hello.tsx
Created August 25, 2018 11:47
InversifyJS with React, listing 2
import * as React from "react";
import { lazyInject } from "./ioc";
import { IProvider } from "./providers";
export class Hello extends React.Component {
@lazyInject("nameProvider") private readonly nameProvider: IProvider<string>;
render() {
return <h1>Hello {this.nameProvider.provide()}!</h1>;
}
@tswistak
tswistak / Hello.tsx
Last active August 25, 2018 08:55
InversifyJS with React, listing 1
import * as React from "react";
import { IProvider } from "./providers";
export class Hello extends React.Component {
private readonly nameProvider: IProvider<string>;
render() {
return <h1>Hello {this.nameProvider.provide()}!</h1>;
}
}