Skip to content

Unsoundness with strictNull, spread operators and accessors #38824

Closed
@skogsbaer

Description

@skogsbaer

TypeScript Version: 3.9.3

Search Terms: strictNull spread

Code

// bug.ts
function useObj(obj: {foo: string, bar: number}): string {
    return obj.foo;
}

interface Inter {
    foo: string;
    spam: number;
}
class C implements Inter {
    get foo() {
        return "foo string";
    }

    get spam() {
        return 42;
    }
}

function doWork(c: Inter) {
    const res = useObj({...c, bar: c.spam}); // <==
    console.log("res=" + res);
}

doWork(new C()); // should print "res=foo string"

Expected behavior: should print res=foo string or result in a type error saying that property foo is undefined in the line marked with <==

Actual behavior: prints "res=undefined"

$ tsc --target ES5 --strictNullChecks bug.ts
$ node bug.js
res=undefined

Playground Link:

Related Issues: no

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions