Skip to content

Instantly share code, notes, and snippets.

View dooman87's full-sized avatar
🤟
Having fun!

Dmitry N. Pokidov dooman87

🤟
Having fun!
View GitHub Profile
export function apiKey(state: ApiKeyState, action: BaseAction) : ApiKeyState {
let newState: ApiKeyState;
switch (action.type) {
case 'ADD_API_KEY':
newState = Object.assign({}, state);
newState.data.apiKeys = [
...state.data.apiKeys,
action.payload.Key
] as string[];
@dooman87
dooman87 / Reducer.ts
Last active November 26, 2018 10:39
FIX: TS2454: Variable 'newState' is used before being assigned.
export function apiKey(state: ApiKeyState, action: BaseAction) : ApiKeyState {
//Assigning variable here.
let newState: ApiKeyState = state;
switch (action.type) {
case 'ADD_API_KEY':
newState = Object.assign({}, state);
newState.data.apiKeys = [
...state.data.apiKeys,
action.payload.Key
@dooman87
dooman87 / Component.ts
Last active January 3, 2017 00:27
TS2322:Type undefined is not assignable to type number.
private usedMb(): number {
if (this.props.usedBytes != null) {
return (this.props.usedBytes / (1024 * 1024));
}
//ERROR: TS2322:Type undefined is not assignable to type number.
return undefined;
}
@dooman87
dooman87 / Component.ts
Last active January 3, 2017 00:28
FIX: TS2322:Type undefined is not assignable to type number.
private usedMb(): number | undefined {
if (this.props.usedBytes != null) {
return (this.props.usedBytes / (1024 * 1024));
}
return undefined;
}
@dooman87
dooman87 / Component.ts
Last active January 3, 2017 00:29
TS2531: Object is possibly undefined
private formatUsedMb(): string {
//ERROR: TS2531: Object is possibly undefined
return this.usedMb().toFixed(0).toString();
}
@dooman87
dooman87 / Component.ts
Created January 2, 2017 23:11
Bytes to megabytes.
private usedMb(): number | undefined {
if (this.props.usedBytes) {
return (this.props.usedBytes / (1024 * 1024));
}
return undefined;
}
@dooman87
dooman87 / Types.ts
Created January 2, 2017 23:15
Used defined type guards.
export function isNumber(n: any): n is number {
return typeof n === "number";
}
@dooman87
dooman87 / Component.ts
Created January 2, 2017 23:18
Bytes to megabytes using type guard.
private usedMb(): number | undefined {
//Using type guard isNumber()
return isNumber(this.props.usedBytes) ?
(this.props.usedBytes / (1024 * 1024)) :
undefined;
}
@dooman87
dooman87 / FishOrBird.ts
Created January 2, 2017 23:23
Advanced type guards.
interface Bird {
fly();
layEggs();
}
interface Fish {
swim();
layEggs();
}
@dooman87
dooman87 / Component.ts
Created January 3, 2017 00:31
FIX: possibly undefined.
private formatUsed(): string {
let usedMb = this.usedMb();
return usedMb ? usedMb.toFixed(0).toString() : '';
}