Last active
June 28, 2018 09:04
-
-
Save geistchevalier/6fb6221a24594220991a89ce354d52cc to your computer and use it in GitHub Desktop.
Typescript definitions / @types : examples for different kinds of functions
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
// !! DISCLAIMER !! I'm still a beginner at Typescript and only mediocre at javascript. | |
// If you see any sort of mistake please do feel free to comment. | |
// This gist was created because I was trying to figure out how to port out | |
// some old js codes to typescript and I had to deal with libraries with no @types files. | |
/*------------------------------------------*/ | |
// BEFORE YOU START | |
// If you want to totally ignore with the whole typing thing for functions | |
// Just declare this in your Typescript file | |
declare var function_name_here: any; // if the function returns something | |
declare var function_name_here: void; // if the function returns nothing | |
// WHY? | |
// -To stop the annoying warnings in compilers nagging you about not finding the function | |
// -For the intellisense | |
/*------------------------------------------*/ | |
// | |
// normal function | |
// | |
function normal_example(var1, var2, var3){ | |
return var1 + var2 + var3; | |
} | |
// usage | |
var result = normal_example(1,2,3); | |
//////////////// | |
// definition | |
//////////////// function_name ///----------- arguments ---------------// output type | |
declare function normal_example(var1: number, var2: number, var3: number): number; | |
/*------------------------------------------*/ | |
// | |
// function that outputs a custom object | |
// | |
function object_output(var1){ | |
return new cat{ ammount: var1 }; | |
} | |
// usage | |
object_output(1).enable(); | |
//////////////// | |
// definition | |
//////////////// function_name /// args ///// output type | |
declare function object_output(var1: number): cat; | |
//output objects needs the class to be defined | |
declare class cat{ | |
enable(): void; | |
disable(): void; | |
} | |
/*------------------------------------------*/ | |
// | |
// functions inside of classes | |
// | |
var something = function(context) { | |
if(this.__proto__.constructor !== formgen) { | |
return new formgen(context); | |
} | |
this.createSomething = function (var1){ | |
return var1; | |
} | |
} | |
// Usage | |
var a_thing = new something(); | |
var new_thing = a_thing.createSomething("Hello World"); | |
///////////// | |
// definition | |
///////////// class-name | |
declare class something { | |
// constructor | |
constructor(); | |
// function name // args ///// output | |
createSomething(input: string): string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment