Skip to content

Extra properties does not trigger error when spreading object #43499

@hansott

Description

@hansott

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions