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

Excess properties allowed in function weak return types #55078

Closed
michaldudak opened this issue Jul 19, 2023 · 4 comments
Closed

Excess properties allowed in function weak return types #55078

michaldudak opened this issue Jul 19, 2023 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@michaldudak
Copy link

Bug Report

πŸ”Ž Search Terms

excess, extra

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about excess properties

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface EmptyInterface {}

interface NonemptyInterface { _?: unknown }

interface SomeOtherInterface {
    x?: string;
}

type ObjectOrCallback<T> =
  | (SomeOtherInterface & T)
  | (() => (SomeOtherInterface & T));

// @ts-expect-error
const a: ObjectOrCallback<EmptyInterface> = { excessProp: 1 };

const fa: ObjectOrCallback<EmptyInterface> = () => ({ excessProp: 1 }); // why doesn't this cause an error too?

// @ts-expect-error
const b: ObjectOrCallback<NonemptyInterface> = { excessProp: 1 };

// @ts-expect-error
const fb: ObjectOrCallback<NonemptyInterface> = () => ({ excessProp: 1 });

πŸ™ Actual behavior

The function fa, which is defined as having the return type { x?: string } & {} can return an object with extra properties, whereas function fb, having the return type { x?: string } & { _?: unknown} cannot. This seems to be inconsistent.

πŸ™‚ Expected behavior

Since both EmptyInterface & SomeOtherInterface and NonemptyInterface & SomeOtherInterface are types with optional properties only, both fa and fb should not allow excess properties on their return type.

@MartinJohns
Copy link
Contributor

// why doesn't this cause an error too?

Because your function has no return type annotation, so the return type is inferred. Then the type of your function is checked for assignability, in which case excess property checks are not occurring anymore. This is working as intended, and there are numerous issues about this.

@michaldudak
Copy link
Author

Why the behavior of the fb function is different, though?

@RyanCavanaugh
Copy link
Member

Duplicate #241

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 20, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2023
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