Skip to content

Instantly share code, notes, and snippets.

View tswistak's full-sized avatar

Tomasz Świstak tswistak

View GitHub Profile
@tswistak
tswistak / tsconfig.json
Created July 30, 2018 06:17
TypeScript 3.0, listing 11
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2015", "dom"],
"outDir": "../../lib/client",
"composite": true,
"strict": true,
"esModuleInterop": true
},
@tswistak
tswistak / defaultProps.ts
Created July 30, 2018 06:17
TypeScript 3.0, listing 10
type Props = {
title?: string;
};
class Heading extends React.Component<Props> {
static defaultProps = {
title: 'Hello!'
};
render() {
const title = this.props.title;
@tswistak
tswistak / defaultProps.ts
Created July 30, 2018 06:16
TypeScript 3.0, listing 9
type Props = {
title?: string;
};
class Heading extends React.Component<Props> {
static defaultProps = {
title: 'Hello!'
};
render() {
const title = this.props.title;
@tswistak
tswistak / defaultProps.js
Created July 30, 2018 06:15
TypeScript 3.0, listing 8
import * as React from 'react';
import * as ReactDOM from 'react-dom';
class Heading extends React.Component {
render() {
return <h1>{this.props.title.toUpperCase()}</h1>
}
}
Heading.defaultProps = { title: 'Hello!' };
@tswistak
tswistak / unknown.ts
Created July 30, 2018 06:14
TypeScript 3.0, listing 7
const a1: any = 'a';
const a2: unknown = 'a';
a1.length; // = 1
a2.length; // compiler error
a1(); // error during execution
a2(); // compiler error
new a1(); // error during execution
new a2(); // compiler error
const a3 = a1 + 3; // = 'a3'
const a4 = a2 + 3; // compiler error
@tswistak
tswistak / params.js
Created July 30, 2018 06:13
TypeScript 3.0, listing 6
function compose2<T1 extends any[], T1R, T2>(f1: (...args1: T1) => T1R, f2: (arg: T1R) => T2) {
return (...a: T1) => f2(f1(...a));
}
const add = (x: number, y: number) => x + y;
const sqr = (x: number) => x * x;
const addAndSqr = compose2(add, sqr);
addAndSqr(1, 2); // valid
addAndSqr('a', 2); // invalid type
@tswistak
tswistak / params.js
Created July 30, 2018 06:12
TypeScript 3.0, listing 5
function hello(...args: [string, number, number, ...string[]]): string {
return args.join(',');
}
@tswistak
tswistak / params.js
Last active July 31, 2018 11:19
TypeScript 3.0, listing 4
// JavaScript
function jsHello() {
return [...arguments].join(',');
}
// JavaScript, second way
function jsHello1(...args) {
return args.join(',');
}
@tswistak
tswistak / tuples.ts
Created July 30, 2018 06:11
TypeScript 3.0, listing 3
type Strings = [...string];
type Empty = [];
const c1: Strings = ['a', 'b'];
const c2: Strings = [];
const c3: Empty = [];
const c4: Empty = ['a', 'b']; // invalid type
@tswistak
tswistak / tuples.ts
Created July 30, 2018 06:10
TypeScript 3.0, listing 2
type StringAndNumbers = [string, ...number[]];
const b1: StringAndNumbers = ['a', 1, 2, 3];
const b2: StringAndNumbers = ['a'];