6

Closed

Please add named-parameter support at call site

description

See:


function doSomething(name:string, age?:number, height?:number) {
}

doSomething("James", height:5);

// Generated code:

doSomething("James", undefined, 5);
Closed Feb 6 at 8:39 PM by jonturner
Thanks for the suggestion. I agree it can be nice in languages that support it.

In TypeScript, we're trying to stay compatible with JavaScript. This means we're trying to limit any addition to syntax at the expression level. Also, in this particular feature, it's actually unclear how to implement it in the general case. If the type of doSomething is any, we wouldn't know how to reorder arguments without runtime reflection.

comments

rwaldron wrote Oct 15, 2012 at 6:44 PM

Even as a superset feature, this won't parse. Just use an object:

declare var params: {
name : any;
age: number;
height: number;
}
//var params = { name:string; age:number; height:number; };

function doSomething( params ) {
console.log( arguments );
}

doSomething({ name: "James", height: 5 });



Compiles to:


function doSomething(params) {
console.log(arguments);
}
doSomething({
name: "James",
height: 5
});

rwaldron wrote Oct 15, 2012 at 6:44 PM

Ack, ignore that line with "//var..."

stuartcarnie wrote Oct 24, 2012 at 4:06 PM

Not sure I follow – obviously it won't parse unless it is added to the compiler… I know I can use objects, but for simple functions it increases readability and doesn't require an object be allocated or superfluous code written.

Today I can do:

interface Params {
name:string;
age?:number;
height?:number;
}

class Person {
constructor(params:Params) { }
}

var a = new Person({ name:"James", height:5});

vs

class Person {
constructor(name:string, age?:number, height?:number) { }
}

var a = new Person("James", height:4);