Skip to content
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

Array objects error out when setting a property #43233

Closed
tiagovtristao opened this issue Mar 13, 2021 · 8 comments
Closed

Array objects error out when setting a property #43233

tiagovtristao opened this issue Mar 13, 2021 · 8 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@tiagovtristao
Copy link
Contributor

Bug Report

Array objects error out when setting a property.

πŸ”Ž Search Terms

"array object"

πŸ•— Version & Regression Information

I've tested it in both v3 and v4.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/DYUwLgBAZg9jEF4IG0C6BuAUJ2MB0AtgJ4AKATjAA6IQCM6QA

πŸ’» Code

let foo = [];

foo.myProp = 1;

πŸ™ Actual behavior

"Property 'myProp' does not exist on type 'any[]'."

πŸ™‚ Expected behavior

It shouldn't error out.

@MartinJohns
Copy link
Contributor

You probably need to expand on that. Why do you think it shouldn't error, when the type definition declares no such property?

@tiagovtristao
Copy link
Contributor Author

tiagovtristao commented Mar 13, 2021

I can't easily get that to work without creating some elaborate TypeScript types to make it valid. I feel that shouldn't be the case.

The following is similar for function objects as it works fine:

function foobar() {}

foobar.myProp = 1;

@jcalz
Copy link
Contributor

jcalz commented Mar 13, 2021

This is intentional behavior and not a bug in TypeScript. Perhaps you want a feature request to treat all objects (or maybe just arrays for some reason) as expando objects, the way function statements are currently. I wonder if this request has happened before...

ah, looks like #12416

@MartinJohns
Copy link
Contributor

I can't easily get that to work without creating some elaborate TypeScript types to make it valid.

Not really that elaborate:

type Foo = Array<string> & { myProp?: number };
let foo: Foo = [];
foo.myProp = 1;

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 15, 2021
@tiagovtristao
Copy link
Contributor Author

I can't easily get that to work without creating some elaborate TypeScript types to make it valid.

Not really that elaborate:

type Foo = Array<string> & { myProp?: number };
let foo: Foo = [];
foo.myProp = 1;

That works if you want the properties to be optional.

@tiagovtristao
Copy link
Contributor Author

@RyanCavanaugh, do you know if there's any chance of this being implemented, if I raise a feature request?

I came across this issue when issue https://styled-system.com/responsive-styles#using-objects.

@RyanCavanaugh
Copy link
Member

Unlikely. This would be a rather huge inference revamp for an initialization style that doesn't see much use.

@tiagovtristao
Copy link
Contributor Author

Fair enough, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants