Skip to content
This repository has been archived by the owner on Jul 8, 2018. It is now read-only.

Latest commit

 

History

History
42 lines (30 loc) · 998 Bytes

add.md

File metadata and controls

42 lines (30 loc) · 998 Bytes

add

add public methods to a class (specifically the class'es prototype). A method added in this manner will be overridden by a method with the same name in the class or in an inheriting class.

This is the preferred way to add traits to classes.

API

arguments:

  • methods: an object containing the methods to add
  • targetClass: class to modify

returns: the modified class

methods

a plain object with string keys and function values.

function add<T extends {[k:string]:Function}>(mixin: T):<T1 extends T>(clazz:Class<T1>)=> Class<T1>;
function add<T extends {[k:string]:Function}, T1 extends T>(mixin: T, clazz:Class<T1>):Class<T1>;

Example

function printMessage(text:string){
   console.log(text);
   return 'message printed: '+text;
}

@add({ printMessage })
class Logger{
   printMessage: (text:string)=>string;
}
const logger = new Logger();

calling logger.printMessage('hello') will print:

  • "hello"

and return "message printed: hello"