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

Unsafe type Inference with spread #60878

Open
changchanghwang opened this issue Dec 30, 2024 · 5 comments
Open

Unsafe type Inference with spread #60878

changchanghwang opened this issue Dec 30, 2024 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@changchanghwang
Copy link

changchanghwang commented Dec 30, 2024

πŸ”Ž Search Terms

unsafe type with spread, spread, optional type, type inference

πŸ•— Version & Regression Information

I think all versions

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYGwhgzhAECCBCBhaBvAUNaBLAJgLmggBcAnLAOwHMBuDaMA4sq2u4Ae3KYFdgj2SACjAlKEAinqNSFStAC+ASlR1MRABZYIAOlzQAvNABEARiO1MazTrAH6omxYVo6AdywbhDifQD805jklFUtoDi4iegAjYDtyAFNXOCRBdFDMbUyNLQAaVUsGezFtMDzQpSdMMBjdHDtsnVxK6BJ4om4ScmjgJ3k0PrRw4m64xOTEVIYAcnV4kBB2aFcBEBwppUHOCHYQeO0FykEjVohuECI8Ixzu7XdPFCUSxSA

πŸ’» Code

class ABC {
  id: string;
  a: string;

  constructor(args: { a: string }) {
    this.id = "1";
    this.a = args.a;
  }

  with(args: { a?: string }) {
    const abc = new ABC({
      ...this,
      a: args.a,
    });
    abc.id = this.id;
    return abc;
  }
}

πŸ™ Actual behavior

when I use with method without arguements like below

const abc = new ABC({ a: 'hello world' });
const newAbc = abc.with({});

newAbc.a is undefined.
constructor want a is required but with give constructor undefined.
I think it's not expected working.

πŸ™‚ Expected behavior

It should be caught by compiler. because It's not developer expected working!

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Dec 30, 2024

Note: not a TS team member.

Duplicate #60623, #50559, #10727. this is effectively generic, generic spread is modeled by intersections, which don't adequately capture how properties can be overwritten.

The title and search terms do not adequately summarize or reflect the issue you're facing. If this were not a duplicate, I'd suggest you edit to address it.

@changchanghwang changchanghwang changed the title Compiler can't catch type error Unsafe type Inference with spread Dec 30, 2024
@changchanghwang
Copy link
Author

Note: not a TS team member.

Duplicate #60623, #50559, #10727. this is effectively generic, generic spread is modeled by intersections, which don't adequately capture how properties can be overwritten.

The title and search terms do not adequately summarize or reflect the issue you're facing. If this were not a duplicate, I'd suggest you edit to address it.

sorry, I'm not good at englishπŸ₯²
is it related with generic?
Image

In this manner, if it becomes this & { a: string | undefined; }, then a can ultimately be undefined, so I think it should be inferred as optional, regardless of what this is.

@MartinJohns
Copy link
Contributor

I think it should be inferred as optional

But it's not optional, it's always provided. Optional has different semantics (defined or not defined), which matters with exactOptionalPropertyTypes enabled.

@changchanghwang
Copy link
Author

changchanghwang commented Dec 30, 2024

I think it should be inferred as optional

But it's not optional, it's always provided. Optional has different semantics (defined or not defined), which matters with exactOptionalPropertyTypes enabled.

oh my mistake.
a always provided. (even a is undefined) but It should be inferred as string | undefined.
constructor need string a, but with method can provide undefined
It's weird unsafe type inference

@changchanghwang
Copy link
Author

When TypeScript uses the spread operator, excess property checks can be unintentionally disabled, resulting in unsafe type inference.
It seems like we need an option to οΏ½fix this issue.
Could we handle it similarly to how object literals are type-checked?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 2, 2025
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