Please add named-parameter support at call site



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

doSomething("James", height:5);

// Generated code:

doSomething("James", undefined, 5);
Closed Feb 6, 2013 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.


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) {
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 {

class Person {
constructor(params:Params) { }

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


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

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

SebProd wrote Oct 15, 2013 at 5:01 PM

I understand the reasons you mentioned, but even though using a litteral object can help, it doesn't support default parameters. it would be great to be able to do:

a: boolean; // How do I give a default value?
b: boolean; // How do I give a default value?
c: boolean; // How do I give a default value?
d: boolean; // How do I give a default value?


and be able to call it like myFunction({ c: true })


MgSam wrote Oct 15, 2013 at 8:43 PM

This issue is a duplicate of https://typescript.codeplex.com/workitem/859.

@jonturner I can understand closing this because it's a duplicate, but the stated reasons for closing don't make sense:

1) Keeping syntax like JS's - There will be further development of this language beyond v1.0, right? You guys have already made it clear you will be adding new syntax (as in async/await). Adding named parameters is arguably a smaller syntax change than some of the stuff already in TypeScript.

2) As I stated in the linked issue, this would necessarily be a compile-time only feature, just like 99% of everything else in TypeScript. If the compiler cannot safely rewrite the callsite at compile-time (as in the case of a function typed as any), it would have to throw an error.

Finally, take a look at your own source code. You guys have placeholder comments for named parameters all over the place, in the style of:
function foo(a: string, b: number, importantParam: boolean){ }

foo('blue', 5, /* importantParam: */ true);