Created
December 8, 2012 01:32
-
-
Save jwmcpeak/4238067 to your computer and use it in GitHub Desktop.
TypeScript Overload with No Parameters
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
// constructor | |
class Foo { | |
constructor (); | |
constructor (parm: string); | |
constructor (obj?: any) { | |
// implementation | |
} | |
} | |
// function | |
function foo(): void; | |
function foo(x: string): void; | |
function foo(y: number): void; | |
function foo(z?: any) { | |
if (typeof z === "string") { | |
} else if (typeof z === "number") { | |
} else { | |
} | |
} | |
// method | |
class Foo { | |
foo(): void; | |
foo(name: string): void; | |
foo(obj?: any) { | |
// implementation | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This optional parameter approach is much better. Setting a default value of undefined resulted in the following JavaScript:
As an aside, I thought I tried optional parameters in my initial testing. Apparently I did it wrong...