• Can specify a string literal in place of a type name (for a parameter).
	interface Foo {
	   createElement(name: string, options?: Stuff): Element;
	   createElement(name: 'div', options?: DivStuff): HTMLDivElement;
	   createElement(name: 'img'): HTMLImageElement;
	var s = 'img';
	createElement(s) // ambiguous unless the string overload added above
	class Bar implements Foo {
	  createElement(name: string): Element {
	    if(name === 'div') return new HTMLDivElement();
	    if(name === 'img') return new HTMLImageElement();
	    else return new HTMLCustomElement();
	var f: (s: 'hello') => void  = function(s: string) {}; // invalid
	var f: { (s: 'hello'): void } = function(s: string) {}; // invalid
  • Any overloaded signature that includes at least one string literal is a 'specialized signature'
  • Each specialized signature must be assignment compatible to at least one non-specialized signature
  • Specialized signatures are ignored for assignment compatibility to the type
  • Overload resolution rules need to handle string literals
  • Contextual typing always uses only the non-specialized overloads

Last edited Jan 4, 2013 at 5:53 PM by jonturner, version 6


irezvov Jan 19, 2013 at 7:57 AM 
I proprosed another more common solution in http://typescript.codeplex.com/discussions/428482:
declare type JQueryEvent;

declare type JEventClick: JQueryEvent = 'click';

declare type JEventComplex: JQueryEvent = ['click', 'hover'];

interface JQuery {
bind(event: JEventClick, fn: (e: ClickEvent) => any): JQuery;
bind(event: JQueryEvent, fn: (e: Event) => any)): JQuery;

$('body').bind(JEventClick, (e) => console.log('click at ' + e.x)); // ok

$('body').bind(JEventComplex, (e) => console.log('some event')); // ok

$('body').bind(JQueryEvent, (e) => false); // compile time error - JQueryEvent don't has emmited value