Skip to content

Function arguments desctructuring #4548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
NekR opened this issue Aug 30, 2015 · 6 comments
Closed

Function arguments desctructuring #4548

NekR opened this issue Aug 30, 2015 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@NekR
Copy link

NekR commented Aug 30, 2015

This does not work. This is expected since I have "noImplicitAny": true:

function test({ x, y }) {
  console.log(x, y);
}

test({
  x: 1,
  y: 2
});

src\main.ts(15,17): error TS7005: Variable 'x' implicitly has an 'any' type.
src\main.ts(15,20): error TS7005: Variable 'y' implicitly has an 'any' type.

However, function test({ x:number, y:number }) { does not works too, while function test({ x = 0, y = 0 }) { works fine, but I do not want default params here. Is there a solution for it?

@xLama
Copy link

xLama commented Aug 30, 2015

Which version of TS?
It works fine in 1.7.X

@NekR
Copy link
Author

NekR commented Aug 30, 2015

Latest stable I believe. 1.5.3
On Aug 30, 2015 9:15 PM, "JCLama" [email protected] wrote:

Which version of TS?


Reply to this email directly or view it on GitHub
#4548 (comment)
.

@xLama
Copy link

xLama commented Aug 30, 2015

In this example:

function test({ x, y }) {
  console.log(x, y);
}

test({
  x: 1,
  y: 2
});

It showed "src\main.ts(15,17): error TS7005: Variable 'x' implicitly has an 'any' type." in 1.5.3 but now it is fixed in 1.7.0

And I don´t know what you can do with this

function test({ x:number, y:number }) { } 

You can´t define explicit types in destructuring. That code says:

" I expect two properties ( x and y ) but I rename them to number and number, respectively"
But It can´t because of duplicated.

@NekR
Copy link
Author

NekR commented Aug 30, 2015

How it might be fixed if I use "noImplicitAny"? It should warn as it does.

Right, I cannot define type in destructuring, this is a problem. You may
call it a bug, if you wish.

What I can do with that number properties? Use them as number variables, as
expected, right?
On Aug 30, 2015 10:07 PM, "JCLama" [email protected] wrote:

In this example:

function test({ x, y }) {
console.log(x, y);
}

test({
x: 1,
y: 2
});

It showed "src\main.ts(15,17): error TS7005: Variable 'x' implicitly has
an 'any' type." in 1.5.3 but now it is fixed in 1.7.0

And I don´t know what you can do with this

function test({ x:number, y:number }) { }

You can´t define types in destructuring.


Reply to this email directly or view it on GitHub
#4548 (comment)
.

@xLama
Copy link

xLama commented Aug 30, 2015

As you can´t define type in destructuring, It has no sense that compiler shows an error if you set noImplicitAny to true.

I don´t think that is a bug. ES6 sintax uses " : " to rename property and it interferes with TS typed sintax.
To use type it could provide other way:

function test({ x:number:renamedX , y:number:renamedY}) { } // Very very ugly 

Maybe could be...:

function test({ x:renamedX => number , y:renamedY => number}) { } // Very ugly too.

So, you can´t, for the moment

Yeah, you can:

function test({ x, y }) {
  console.log( x + y );
}

test({
  x: 1,
  y: 2
});

@ahejlsberg ahejlsberg added the Question An issue which isn't directly actionable in code label Aug 30, 2015
@ahejlsberg
Copy link
Member

You can't have type annotations within a destructuring object literal because ES6 already defines a different meaning for : in a destructuring pattern. You can however have an annotation for the entire destructuring parameter:

function test({ x, y }: { x: number, y: number }) {
  console.log(x, y);
}

test({
  x: 1,
  y: 2
});

See also #3828.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants