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

object getters return type error #58483

Closed
lqzhgood opened this issue May 9, 2024 · 12 comments
Closed

object getters return type error #58483

lqzhgood opened this issue May 9, 2024 · 12 comments
Labels
Duplicate An existing issue was already created

Comments

@lqzhgood
Copy link

lqzhgood commented May 9, 2024

type T = {
  data: {
    a: number;
  };
};

const b: T = {
  data: {
    a: 123123,
    b: 3444,    // throw type error
  },
};

const c: T = {
  get data() {
    return {
      a: 123,
      b: 333, // not throw type error
    };
  },
};


const d = {
  get data() {
    return {
      a: 123,
      b: 333 
    };
  },
};

type td = typeof  d; // { a:number, b:number }
 

c.data.b not throw type error seems to be a mistake ?

@lqzhgood lqzhgood added the Duplicate An existing issue was already created label May 9, 2024
@whzx5byb
Copy link

whzx5byb commented May 9, 2024

See https://github.com/microsoft/TypeScript/wiki/FAQ#indirect-excess-properties-are-ok

@fatcerberus
Copy link

fatcerberus commented May 9, 2024

Sigh... I really wish the wording of this error message would be changed.

This isn't a bug. The excess property check error is not a type error, it's a lint check. You can't rely on it to prevent extra properties from being put on an object because it won't always trigger (which is by design). If that's what you need, see #12936.

The other issue you linked to this (#49511) is unrelated - there is nothing like an any involved here. { a: number, b: number } is a legal subtype of and assignable to { a: number }.

@snarbies
Copy link

snarbies commented May 9, 2024

Indirect excess properties are not an error. Examples of indirect excess properties would be:

  • values returned by functions
  • values stored in intermediate variables

b is direct. You go straight from value→property.

c in indirect because it is one step removed. You have a getter, which is a function that returns a value.

From the linked FAQ, excess property checks are to defend against things like typos:

const p: Dimensions = {
    width: 32,
    height: 14,
    depht: 11 // <-- typo!!
}

But there are plenty of situations where you want or need to allow extra properties.

@whzx5byb
Copy link

whzx5byb commented May 9, 2024

When inspecting the code I do find something looks like a bug: #58484.
@lqzhgood , is it what you are talking about?

@lqzhgood
Copy link
Author

lqzhgood commented May 9, 2024

Sigh... I really wish the wording of this error message would be changed.

This isn't a bug. The excess property check error is not a type error, it's a lint check. You can't rely on it to prevent extra properties from being put on an object because it won't always trigger (which is by design). If that's what you need, see #12936.

The other issue you linked to this (#49511) is unrelated - there is nothing like an any involved here. { a: number, b: number } is a legal subtype of and assignable to { a: number }.

The focus of this issue is not on whether additional attributes can be added,
Is difference types of b.data and c.data by use getters
b.data and c.data should throw errors simultaneously or not

@lqzhgood
Copy link
Author

lqzhgood commented May 9, 2024

When inspecting the code I do find something looks like a bug: #58484. @lqzhgood , is it what you are talking about?

yes,
d.data and c.data (getter) Inconsistent types returned,(from the perspective of throwing exceptions)
and c.data looks like any in autocomplete

@lqzhgood
Copy link
Author

lqzhgood commented May 9, 2024

Indirect excess properties are not an error. Examples of indirect excess properties would be:

  • values returned by functions
  • values stored in intermediate variables

b is direct. You go straight from value→property.

c in indirect because it is one step removed. You have a getter, which is a function that returns a value.

From the linked FAQ, excess property checks are to defend against things like typos:

const p: Dimensions = {
    width: 32,
    height: 14,
    depht: 11 // <-- typo!!
}

But there are plenty of situations where you want or need to allow extra properties.

Thank you for your answer, but c.data look like any in autocomplete.

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAKlC8UDeAoKUAmBDYWBcya6U+UAdgK4C2ARhAE4DcR6AXuwZbQ8+gL7MBKFAGMA9mQDOwKDQJxEqdNlwElxUgEYATAGYdugDQtZBXQBZLh4lAD0tqMAAW9MQHdH4aA1f0T7VgIDIj5jIVEJaSgReQRCdABzCBkVLAAKAEp44npkinoybJstPWMbdDkoXWrre3IxGWdXD1BIKB8xP3KodhMhfjDmYXEpFLj1JJScdKz1dFzgfMK5jSDSkwqzaqg+3ihQlHDW6GAMOOOxADNlRjsHJBI8Ljp6azlnhn2gA

image

@fatcerberus
Copy link

You are conflating two different things. The autocomplete thing is unrelated to the type checking of c.data.

b.data and c.data should throw errors simultaneously or not

No, they shouldn't; it's already been explained several times why c.data doesn't have an error. The excess property check doesn't apply to it, but it's still type-checked properly. For example if you change the code to

const c: T = {
  get data() {
    return 42;
  },
};

then you get a type error.

@lqzhgood
Copy link
Author

lqzhgood commented May 9, 2024

You are conflating two different things. The autocomplete thing is unrelated to the type checking of c.data.

b.data and c.data should throw errors simultaneously or not

No, they shouldn't; it's already been explained several times why c.data doesn't have an error. The excess property check doesn't apply to it, but it's still type-checked properly. For example if you change the code to

const c: T = {
  get data() {
    return 42;
  },
};

then you get a type error.

Thank you, I understand my mistake
The real problem should be automatic completion
The problem of automatic completion made me think that c.data had changed to any type, resulting in the loss of type checking

@fatcerberus
Copy link

In that case, this is a retroactive duplicate of #58484 😄

@lqzhgood
Copy link
Author

lqzhgood commented May 9, 2024

the real problem is #58484. I will close this issue

@lqzhgood lqzhgood closed this as completed May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants