-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Destructuring index signature by computed property loses type information #17566
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
Comments
You've said that |
This is NOT working as intended. If every property on TSC handles example 2 correctly but example one incorrectly. That's the problem. (my real use case is more of a deep structure where this constantly requires me to declare additional variables in order to access the structures I care about). |
Please remove the label 'working as intended' as it most certainly is not.
As said, if it was, the behaviors would be identical and they're not.
…On Wed, Aug 2, 2017, 12:16 PM Daniel Rosenwasser ***@***.***> wrote:
You've said that test has an index signature; you're basically ignoring
properties on the RHS and guaranteeing that any property on test is a
FooBar.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#17566 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACBnil70JVXXWXLTZiBBwyhp8eSiQ6C9ks5sUK7dgaJpZM4OrSZo>
.
|
@amirburbea providing a simpler example would be more useful than demanding that we apply our triage process differently @DanielRosenwasser I don't think this is correct? Example type Points = { [key: string]: { x: number, y: number } };
const Points: Points = <any>null;
const { ['a']: { x: var_x1, y: var_y1, z: var_z1 } } = Points;
const A: 'a' = 'a';
const { [A]: { x: var_x2, y: var_y2, z: var_z2 } } = Points; |
Thank you @RyanCavanaugh (and my sincere apologies to you and @DanielRosenwasser if I went about this the wrong way). Your example does perfectly illustrate the same issue - property |
@RyanCavanaugh, I hate to press but seeing as the 'Working as Intended' label is still on the ticket, should I be worried that this bug won't get addressed? The bug is displayed in an even worse manner in the sample below. const foobar = {} as { [key: string]: { zomg: number } };
const { ['l']: { zomg: a1 } } = foobar;
// test1 is correctly typed as a number - attempting to assign something else than a number would not be allowed.
const test1: typeof a1 = 6;
const { [test1.toString()]: { zomg: a2 } } = foobar;
// test2 is incorrectly typed as any - allowing me to assign a string even though it should not.
const test2: typeof a2 = ''; |
any progress? or do you want to tell me where I should start looking if i'd want to perhaps investigate this myself as my first PR? |
No, I haven't had a chance to look at this one. You could start by looking at |
Any updates on this? Running into same issue. |
@levi217 my advice for fixing this is the same as to @amirburbea. |
@RyanCavanaugh this issue can be closed, it was fixed long time ago by #23489 |
TypeScript Version: 2.4.2
Code
Expected behavior:
Both examples (destructure by
[key]
and destructure by['b']
) should yield identical behavior - in this case an error because there is noy
in typeZomg
Actual behavior:
Example 1 is allowed to proceed and more importantly loses all type information about the destructured variables (so
x1
isany
instead ofnumber
whereasx2
correctly is inferred as anumber
).The text was updated successfully, but these errors were encountered: