Skip to content

[Bug] Result of object rest isn't assignable to symbol index signature #49235

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

Closed
sounisi5011 opened this issue May 25, 2022 · 3 comments Β· Fixed by #49337
Closed

[Bug] Result of object rest isn't assignable to symbol index signature #49235

sounisi5011 opened this issue May 25, 2022 · 3 comments Β· Fixed by #49337
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@sounisi5011
Copy link

Bug Report

πŸ”Ž Search Terms

  • PropertyDescriptorMap
  • PropertyDescriptor
  • Spread

πŸ•— Version & Regression Information

  • This has changed since version 4.6

⏯ Playground Link

Playground link with relevant code:

πŸ’» Code

declare const properties1: { [x: string]: PropertyDescriptor };

// This will pass. This is because the type '{ [x: string]: PropertyDescriptor }' can be assigned to the type 'PropertyDescriptorMap'.
Object.defineProperties({}, properties1);

// But when it passes the spread syntax, it fails even though the types are the same.
const { ...properties2 } = properties1;
Object.defineProperties({}, properties2);

πŸ™ Actual behavior

I get the following error:

Argument of type '{ [x: string]: PropertyDescriptor; }' is not assignable to parameter of type 'PropertyDescriptorMap & ThisType<any>'.
  Type '{ [x: string]: PropertyDescriptor; }' is not assignable to type 'PropertyDescriptorMap'.
    Index signature for type 'symbol' is missing in type '{ [x: string]: PropertyDescriptor; }'.

But, type { [x: string]: PropertyDescriptor; } is assignable to type PropertyDescriptorMap. Objects copied with spread syntax cannot be assigned even though they have the same type.

πŸ™‚ Expected behavior

This type error does not occur. For the same type, it should be consistently assignable / non-assignable anywhere.

@sounisi5011 sounisi5011 changed the title Objects that pass the spread syntax can no longer be assigned to assignable types. [Bug] Objects that pass the spread syntax can no longer be assigned to assignable types. May 25, 2022
sounisi5011 added a commit to sounisi5011/npm-packages that referenced this issue May 25, 2022
* chore(deps): update dependency typescript to v4.7.2

* chore(check-pid-file): disable type errors due to TypeScript bugs

    see microsoft/TypeScript#49235

* chore(monorepo-workspace-submodules-finder-action): add the `--transpile-only` flag to avoid errors

    When using TypeScript 4.7.2 with `@vercel/[email protected]`, I get the following error:

        Error: Module build failed (from ../../node_modules/.pnpm/@vercel[email protected]/node_modules/@vercel/ncc/dist/ncc/loaders/ts-loader.js):
        Error: Debug Failure. False expression: Non-string value passed to `ts.resolveTypeReferenceDirective`, likely by a wrapping package working with an outdated `resolveTypeReferenceDirectives` signature. This is probably not a problem in TS itself.

    To suppress this error, I added the `--transpile-only` flag to the build command.

Co-authored-by: Renovate Bot <[email protected]>
Co-authored-by: Sonishi Izuka <[email protected]>
@MartinJohns
Copy link
Contributor

I don't know if this is intentional, but a quick workaround is to write { [x: string | symbol]: PropertyDescriptor }.

@sounisi5011
Copy link
Author

Yes. But in the actual code, we may use the spread syntax when we want to exclude some properties.

https://github.com/sounisi5011/npm-packages/blob/fdf42d3f512f5ed3cb24ee7c5056f96eeafbd014/packages/check-pid-file/src/index.ts#L15

The same error occurs in this case as well. Such code would require a type hack like the one below to set the proper type:

const oldErrorProperties = Object.getOwnPropertyDescriptors(oldError);
const { message: _1, stack: _2, ...newErrorProperties } =
    oldErrorProperties as (
        & typeof oldErrorProperties
        & { [x: symbol]: PropertyDescriptor }
    );
Object.defineProperties(newError, newErrorProperties);

Playground

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 25, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone May 25, 2022
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label May 25, 2022
@RyanCavanaugh
Copy link
Member

Shorter repro:

const { ...properties2 } = {};
// Fails, should pass
const t1: { [x: symbol]: unknown } = properties2;
// OK
const t2: { [x: symbol]: unknown } = {};

@sandersn sandersn changed the title [Bug] Objects that pass the spread syntax can no longer be assigned to assignable types. [Bug] Object rest of string index signature isn't assignable to symbol index signature Jun 9, 2022
@sandersn sandersn changed the title [Bug] Object rest of string index signature isn't assignable to symbol index signature [Bug] Result of object rest isn't assignable to symbol index signature Jun 9, 2022
gabritto pushed a commit that referenced this issue Jun 14, 2022
…ned to assignable types. (#49337)

* fix issue 49235

* update code with suggested changes

Co-authored-by: Benjamin Tong <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
3 participants