Skip to content

Instantly share code, notes, and snippets.

@NimaAra
Last active May 27, 2019 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NimaAra/2395511e490c7ba829df7c54e62ff3c5 to your computer and use it in GitHub Desktop.
Save NimaAra/2395511e490c7ba829df7c54e62ff3c5 to your computer and use it in GitHub Desktop.
A set of useful resources related to working with TypeScript

LINQ->TypeScript

Functions

// a function declaration accepting a number and returning a string.
let someFunc: (age: number) => string;
// a function implementation accepting a number and returning a string.
someFunc = age => "42";
/*
 * a function implementation accepting a string and variable number
 * of arguments as `ages`.
 * funcWithRest("Foo", 1);
 * funcWithRest("Foo", 1, 2);
*/
function funcWithRest(name: string, ...ages: number[]) {}

Enums

enum CategoryA { One, Two, Three}; // 0, 1, 2
enum CategoryB { One = 1, Two = 2, Three = 3 }; // 1, 2, 3

let someCategory: CategoryB = CategoryB.One; // 1
let categoryStr = CategoryA[someCategory]; // One

Tuples

let someTuple: [number, string] = [1, "one"];
let left = someTuple[0]; // 1
let right = someTuple[1]; // "one"

Interfaces

interface Book {
    id: number;
    title: string;
    author: string;
    pages?: number;
    markAsDamaged: (reason: string) => void;
    (age: number, text: string): string; // a function declaration
}

Classes

class SomeClass {
    someProperty: string;

    constructor(someName: string, public yetAnotherProp: string) {
        this.someProperty = someName;        
    }

    private _someOtherProperty : string;
    public get someOtherProperty() : string {
        return this._someOtherProperty;
    }
    public set someOtherProperty(v : string) {
        this._someOtherProperty = v;
    }
    
    static someStaticProp: number = 42;
    
    private someMethod(age: number): void {
        console.log(this.yetAnotherProp);        
    }

    // this is a public method without the access modifier.
    someOtherMethod(age: number): void {
        console.log(this.yetAnotherProp);        
    }
}
// class expressions
let FooClass = class extends SomeClass {
    printsFoo(): void {
        console.log("Foo");
        
    }
}

let fooInstance = new FooClass("bar", "baz");

Generics

let numbers: Array<number> = [1, 2, 3];
let moreNumbers = new Array<number>(3);

interface ISomeInterface {}

function someFunction<T extends ISomeInterface>(): void {}

Advanced Generics

declare global {
    interface Array<T> {
        filterWith<TKey extends keyof T, TValue extends T[TKey]>(member: TKey, value: TValue): T[];
    }   
   }

type SomeType = {
    page: number;    
    isOkay: boolean;
};

Array.prototype.filterWith = function <
    T, 
    TKey extends keyof T, 
    TValue extends T[TKey]>(member: TKey, value: TValue): T[] {
        return this.filter(x => x[member] === value);
    }

let stuff: Array<SomeType>;

let foo: SomeType = {
    page: 1,
    isOkay: false
}

let filtered = stuff.filterWith("page", 2);

Modules

// MyModule.ts
export interface ISomething {};
export function someFunction(input: string): number { return 1; }
export class SomeClass {}

// Consumer1.ts
import { someFunction, SomeClass as SomeOtherClass } from "./MyModule";

// Consumer2.ts
import * as stuff from "./MyModule";
let theClass: number = stuff.someFunction("foo");

Namespaces

// MyUtility.ts
namespace Utility {
    export namespace Foo {
        export function someFunction(): void {}
    }

    export function someOtherFunction(): void {}

    function somePrivateFunction(): void {}
}

// Consumer1.ts
/// <reference path="./MyUtility.ts" />
let someFunc = Utility.Foo.someFunction;

// Consumer2.ts
/// <reference path="./MyUtility.ts" />
import util = Utility.Foo;
let someFunc = util.someFunction;

Custom Types

type SomeType = {
    Id: number;
    name: string;
    flag: boolean;
};

let foo: SomeType = {
    Id: 1,
    name: "df",
    flag: false
};

type ExtendedType = SomeType & { age: number};

let bar: ExtendedType = {
    Id: 1,
    name: "df",
    flag: false,
    age: 2
};

var typeKeys: keyof SomeType; // "Id" | "Name" | "flag"

Mapped Types

type SomeType = {
    Id: number;
    name: string;
    flag: boolean;
};

// creates a new type based on another type
type SomeOtherType = {
    [x in keyof SomeType]: SomeType[x];    
};

// creates a new type based on another type
// where all the properties are optional.
type Optional<T> = {
    [x in keyof T]?: T[x];    
};

// creates a new type based on another type
// where all the properties are optional readonly string.
type ReadonlyOptional<T> = {
    readonly [x in keyof T]?: string;
};

type partialType = Partial<SomeType>;
type readonlyType = Readonly<SomeType>;
type pickedType = Pick<SomeType, 'Id' | 'name'>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment