Skip to content
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

Unsoundness with strictNull, spread operators and accessors #38824

Closed
skogsbaer opened this issue May 28, 2020 · 3 comments
Closed

Unsoundness with strictNull, spread operators and accessors #38824

skogsbaer opened this issue May 28, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@skogsbaer
Copy link

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

@jgbpercy
Copy link

Spread tends to introduce a lot of potential unsoundness and should be used with care. There's nothing really in TS currently that addresses the difference in semantics between property access and something like spread or Object.keys (at least as far as I know). Here's another example where spread can land you in trouble with properties that are unset vs explicitly undefined.

While I'd love to see the team address stuff like that at some point, I don't know how far up the priority list it really is, and what the trade offs would be in terms of complexity, perf and "false positives" (rejecting code that is actually fine) for the compiler.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 29, 2020
@RyanCavanaugh
Copy link
Member

This is effectively the same as #9726 (if we did anything, we would handle ownness/enumerability as part of some combined effort to address exactly this scenario)

@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

4 participants