Skip to content

No type error when setting an unknown property of React component using the spread operator #18801

Closed
@green2k

Description

@green2k

TypeScript Version: 2.5.3

Code

interface IFooProps {
  key: string
  value: number
}

interface IBarProps {
  key: string
  value: number
  something: any
}

const Foo = (props: IFooProps) => {
  return (
    <div />
  )
}

const barProps: IBarProps = {
  key: 'foo',
  value: 1,
  something: 'something'
}

const Test1 = () => {
  // I'm setting the `something` property which isn't defined in the `Foo` component (compile-time error). This is a correct behavior.
  return <Foo key={barProps.key} value={barProps.value} something={barProps.something} />
}

const Test2 = () => {
  // However, I can set the `something` property using the spread operator and the TypeScript compiler will not throw any error ---> Is this a bug?
  return <Foo {...barProps} />
}

Expected behavior:

  • Test1 throws the [ts] Property 'something' does not exist on type 'IntrinsicAttributes & IFooProps' error.
  • Test2 throws the [ts] Property 'something' does not exist on type 'IntrinsicAttributes & IFooProps' error.

Actual behavior:

  • Test1 throws the [ts] Property 'something' does not exist on type 'IntrinsicAttributes & IFooProps' error.
  • Test2 throws no error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions