Skip to content

Inference seems to be broken with the spread syntax? #40840

@dolsem

Description

@dolsem

TypeScript Version: 4.0.2

Search Terms:

Expected behavior: No error for both function calls

Actual behavior: The first call succeeds, the second fails

Related Issues:

Code

interface I {
    bla: string;
    blabla: string;
}

const fn = (obj: Record<string, string>) => obj;

const obj: I = { bla: '', blabla: '' };

const { bla, ...rest } = obj;
fn({ ...rest });
fn(rest);
Output
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
const fn = (obj) => obj;
const obj = { bla: '', blabla: '' };
const { bla } = obj, rest = __rest(obj, ["bla"]);
fn(Object.assign({}, rest));
fn(rest);
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Metadata

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