Skip to content

Proxy property in setter has never type #47295

Closed

Description

Bug Report

🔎 Search Terms

  • Proxy property in setter has never type
  • Type 'any' is not assignable to type 'never'.(2322)
  • Typing Proxy Constructor Proxy Handler
  • Typing ProxyConstructor
  • Typing ProxyHandler

🕗 Version & Regression Information

  • This is a crash (misbehavior)

⏯ Playground Link

Playground link with relevant code

💻 Code

const myObj = {
  num: -1,
  arr: [1, 2, 3],
  str: '',
};

const proxy = new Proxy(myObj, {
  set(target, prop: keyof typeof myObj, value) {
    /* none of these works */
    // Ver. 1
    if (prop in target) target[prop] = value;
    // Ver. 2
    target[prop] = value;
    // Ver. 3
    target[prop as keyof typeof myObj] = value;
    // Ver. 4
    switch (prop) {
      case 'num':
      case 'str':
        target[prop] = value;
        break;
    }

    /* while these works */
    // Ver. 5
    switch (prop) {
      case 'num':
        target[prop] = value;
        break;
      case 'str':
        target[prop] = value;
        break;
    }
    // Ver. 6
    switch (prop) {
      case 'num':
        target[prop] = value;
        break;
      default:
        target[prop] = value;
        break;
    }

    return true;
  },
});

console.log('proxy : ', proxy);

🙁 Actual behavior

With TypeScript 4.5.4 (current latest) and currently Nightly version, this code is throwing error Type 'any' is not assignable to type 'never'.(2322) for the version 1,2,3,4 yet for version 5 and 6 no error is reported. If we remove one of the num prop or str, all the errors are gone.

Is there an explanation for this design?

Also asked on stack overflow

🙂 Expected behavior

We can use all of version 1,2,3,4 without having this TS2322 error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions