Skip to content

Extra properties does not trigger error when spreading object #43499

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
hansott opened this issue Apr 2, 2021 · 2 comments
Closed

Extra properties does not trigger error when spreading object #43499

hansott opened this issue Apr 2, 2021 · 2 comments

Comments

@hansott
Copy link

hansott commented Apr 2, 2021

Bug Report

function inferType(value: string) {
    const asBoolean: boolean | null = null;
    const asDate: Date | null = null;
    const asDouble: number | null = null;
    const asLong: number | null = null;
    const asURL: URL | null = null;

    return {
        value_as_string: value,
        value_as_boolean: asBoolean,
        value_as_date: asDate,
        value_as_double: asDouble,
        value_as_long: asLong,
        value_as_url: asURL,
  };
}

type EventMetaData = {
  key: string;
  value_as_string: string;
  value_as_boolean: boolean | null;
  value_as_date: string | null;
  value_as_long: number | null;
  value_as_double: number | null;
};

function convert(key: string, value: string): EventMetaData {
    const types = inferType(value);

    return {
        key: key,
        ...types // Expect error here because `value_as_url` is not allowed
    };
}

Would expect the following error (or something like that):

Type '{ value_as_url: URL; value_as_string: string; value_as_boolean: null; value_as_date: null; value_as_double: null; value_as_long: null; key: string; }' is not assignable to type 'EventMetaData'.
  Object literal may only specify known properties, and 'value_as_url' does not exist in type 'EventMetaData'.(2322)

🔎 Search Terms

object spread extra properties expected error

🕗 Version & Regression Information

Using TypeScript 4.1.5

⏯ Playground Link

No error playground

Expected error

@MartinJohns
Copy link
Contributor

This is intentional: #19775

@hansott
Copy link
Author

hansott commented Apr 2, 2021

@MartinJohns I see, thanks!

Subscribing to #12936 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants