Skip to content

Wrong type for object assign array #35778

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
Grabauskas opened this issue Dec 19, 2019 · 3 comments
Closed

Wrong type for object assign array #35778

Grabauskas opened this issue Dec 19, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Grabauskas
Copy link

Grabauskas commented Dec 19, 2019

TypeScript Version: 3.8.0-dev.20191218

Search Terms: Object assign spread array

Expected behavior:

const z: {
    '0': 'a';
    '1': 'b';
    '2': 'c';
};

Actual behavior:

const z: {
    [x: number]: never;
    length: number;
    toString(): string;
    toLocaleString(): string;
    pop(): undefined;
    push(...items: never[]): number;
    concat(...items: ConcatArray<never>[]): never[];
    concat(...items: ConcatArray<...>[]): never[];
    ... 24 more ...;
    [Symbol.unscopables](): {
        ...;
    };
}

Code

const z = { ...["a", "b", "c"] };
Output
"use strict";
const z = Object.assign({}, ["a", "b", "c"]);
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 19, 2019
@RyanCavanaugh
Copy link
Member

See #9726

@Grabauskas
Copy link
Author

Grabauskas commented Dec 20, 2019

Maybe at least Object.assign method can be fixed with some overloads?

For example:

interface ObjectConstructor {
    assign<T, U extends (string | unknown[])>(target: T, source: U): T & Record<number, string>;
    assign<T, U extends (boolean | number)>(target: T, source: U): T;
    assign<T, U extends (null | undefined)>(target: T, source: U): T;
    assign<T, U>(target: T, source: U): T & U;
}

Then it will catch errors:

const a: string[] = Object.assign({}, []); // Type 'Record<number, string>' is missing the following properties from type 'string[]': length, pop, push, concat, and 25 more.
const b: string[] = Object.assign({}, "ASD"); // Type 'Record<number, string>' is not assignable to type 'string[]'.

@typescript-bot
Copy link
Collaborator

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

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

3 participants