View commonjs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(a, b) { | |
return a + b; | |
} | |
exports.add = add; | |
function substract(a, b) { | |
return a - b; | |
} |
View main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hasErrorCode = (error: unknown): error is { code: string } => { | |
return !!error && typeof error === 'object' && 'code' in error && typeof error.code === 'string'; | |
}; | |
try { | |
throw {code: 72}; | |
} catch (error: unknown) { | |
if (hasErrorCode(error)) { | |
console.error(`Failed with error code "${error.code}".`); | |
} |
View webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
devtool: 'source-map', | |
entry: { | |
[projectName]: `${__dirname}/${pkg.main}`, | |
}, | |
externals: { | |
dexie: 'Dexie', | |
}, | |
mode: 'production', | |
output: { |
View never.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface StreamResponse { | |
status: StreamStatus; | |
} | |
enum StreamStatus { | |
IDLE, | |
ONLINE, | |
OFFLINE, | |
} |
View Type-Guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Dog = { | |
age: number; | |
name: string; | |
bark: () => void; | |
type: 'dog' | |
} | |
type Person = { | |
age: number; | |
name: string; |
View generic-inheritance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface User { | |
name: string; | |
} | |
interface HappyUser extends User { | |
clap: () => void; | |
} | |
function printName<T extends User>(someone: T): T { | |
console.log(someone.name); |
View generic-interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface HttpResponse<T> { | |
code: number; | |
data: T; | |
} | |
interface ResponseData { | |
completed: boolean; | |
id: number; | |
title: string; | |
userId: number; |
View multiple-generic-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rollDice(array: any[]): number { | |
return Math.floor(Math.random() * array.length); | |
} | |
function getRandoms<A, B>(as: A[], bs: B[]): [A, B] { | |
const a = as[rollDice(as)]; | |
const b = bs[rollDice(bs)]; | |
return [a, b]; | |
} |
View generic-function.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRandom<T>(array: T[]): T { | |
const diceRoll = Math.floor(Math.random() * array.length); | |
return array[diceRoll]; | |
} | |
const names = [ | |
'Amelia', 'Ava', 'Benjamin', 'Charlotte', 'Elijah', 'Emma', | |
'Evelyn', 'Harper', 'Henry', 'Isabella', 'James', 'Liam', 'Lucas', 'Mia', | |
'Noah', 'Oliver', 'Olivia', 'Sofia', 'Theodore', 'William' | |
]; |
View generic-class.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KeyValuePair<KeyType, ValueType> { | |
key: KeyType; | |
value: ValueType; | |
constructor(key: KeyType, value: ValueType) { | |
this.key = key; | |
this.value = value; | |
} | |
} |
NewerOlder