Show Menu
Cheatography

Notizen zu Typescript Cheat Sheet by

Notizen zu Typescript - eher für Anfänger

Overlo­ading von Funkti­ons­sig­naturen

export class Test{
    
    public add( a: Number, b:Number):void;
    public add( a: String, b:String):void;
    public add( a: boolean, b:boolean):void;
    public add( a, b):void{
        console.log(a+b);
    }

    public static run():any{
        let t:Test = new Test();
        t.add('a','b');
        t.add(3,5);
        t.add(true,true);
    }
}

Test.run();

Erzeugt die Ausgaben:
ab 
8
2

Typescript Bindun­gsarten

[xxx]
@Input
Input Parameter
(xxx)
@Output
Callback Parameter
 

Condit­ional Types

declare type StringOrNull<T> = T extends string
  ? string
  : T extends null
  ? null
  : never;

export function upperCase<T extends string | null>(text: T): StringOrNull<T>;
export function upperCase(text: string | null): string | null {
  if (typeof text === 'string') {
    return text.toUpperCase();
  }

  return null;
}

// String
upperCase('').toLocaleLowerCase();

// Null
upperCase(null);

// Union
let maybe: string | null = null as any;
upperCase(maybe);
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Mit Javascript unter Node.js entwickeln Cheat Sheet
          jian's typescript best practices Cheat Sheet

          More Cheat Sheets by FunThomas424242

          Linux - Minihilfe Cheat Sheet