Skip to content

Instantly share code, notes, and snippets.

View bennycode's full-sized avatar
🏡
Working from home

Benny Neugebauer bennycode

🏡
Working from home
View GitHub Profile
@bennycode
bennycode / generic-class.ts
Last active May 16, 2022 20:59
Generic Classes in TypeScript
class KeyValuePair<KeyType, ValueType> {
key: KeyType;
value: ValueType;
constructor(key: KeyType, value: ValueType) {
this.key = key;
this.value = value;
}
}
@bennycode
bennycode / functionOverload.ts
Created May 15, 2022 20:26
Function Overloading
function sum(a: number, b: number): number;
function sum(a: string, b: string): string;
function sum(a: number | string, b: number | string): number | string {
if (typeof a === "number" && typeof b === "number") {
return a + b;
} else {
return `${parseInt(`${a}`, 10) + parseInt(`${b}`, 10)}`;
}
}
Playlist: https://www.youtube.com/playlist?list=PLCbdBdyNHZXLX9p_F8WCPwXHn67QlstZ9
Video in Playlist: https://www.youtube.com/watch?v=qZN0Lo-f3iE&list=PLCbdBdyNHZXLX9p_F8WCPwXHn67QlstZ9
Video: https://www.youtube.com/watch?v=qZN0Lo-f3iE
@bennycode
bennycode / generics.ts
Last active May 15, 2022 22:40
Generics in TypeScript
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', 'Sophia', 'Theodore', 'William'
];
@bennycode
bennycode / sum.ts
Created April 29, 2022 20:01
Function Overloads in TypeScript
function sum(a: number, b: number): number;
function sum(a: string, b: string): string;
function sum(a: number | string, b: number | string): number | string {
if (typeof a === "number" && typeof b === "number") {
return a + b;
} else {
return `${parseInt(`${a}`, 10) + parseInt(`${b}`, 10)}`;
}
}
@bennycode
bennycode / dogOrPerson.ts
Last active April 29, 2022 20:01
Discriminated Unions in TypeScript
interface Dog {
age: number;
name: string;
bark: () => void;
type: "dog";
}
interface Person {
age: number;
name: string;
@bennycode
bennycode / printAddress.test.ts
Last active April 29, 2022 20:02
Type Casting in TypeScript
import {Person, printAddress, MissingAddressError} from './printAddress';
describe('printAddress', () => {
it('throws an error when person does not have an address', () => {
const benny = {
age: 34,
} as Person;
expect(() => {
printAddress(benny);
}).toThrowError(MissingAddressError);
@bennycode
bennycode / tsconfig.json
Created April 12, 2022 09:33
General tsconfig that works with Node.js & React
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"declaration": false,
"downlevelIteration": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
name: 'Merge Dependencies'
# https://github.blog/changelog/2021-02-19-github-actions-workflows-triggered-by-dependabot-prs-will-run-with-read-only-permissions/
on: [pull_request_target]
permissions:
pull-requests: write
contents: write
jobs:
@bennycode
bennycode / is-node.js
Created May 27, 2021 12:50
Is Node.js?
typeof process === 'object'