Skip to content

spreading generic type disables excess property checking #30129

Open

Description

TypeScript Version: 3.3.3333

Search Terms: generic spread

Code

function spread<T extends {foo: string}>(p: T): {foo: string} {
    return {...p, bar: ''};
}

Expected behavior:

Excess property error for bar.

Actual behavior:

No error.

Playground Link: http://www.typescriptlang.org/play/#src=function%20spread%3CT%20extends%20%7Bfoo%3A%20string%7D%3E(p%3A%20T)%3A%20%7Bfoo%3A%20string%7D%20%7B%0D%0A%20%20%20%20return%20%7B...p%2C%20bar%3A%20''%7D%3B%0D%0A%7D

Activity

  1. akozhemiakin commented on Mar 31, 2020

    @akozhemiakin

    The problem is actually more generic, not only about excess properties.

    function assignRandomStuff<T>(a: T): T {
        return { ...a, foo: 5, bar: true};
    }

    I believe this one must not typecheck, but it is.
    That way we can not only add excess properties, but also get invalid object as a result. For example:

    assignRandomStuff({ foo: 'string value' });

    This one returns { foo: number, bar: boolean } but typescript believes it is { foo: string }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions