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
Type error not indicated when spreading into a JSX component whose props type is defined as interface #43760
Comments
|
Are you saying in my code snippet there is another |
|
Wait, so TS allows extending private interfaces that are not exported from modules? And therefore it speculatively assumes that Also, why does TS indicate a type error in |
Hm, TS treats an empty interface differently, somehow? If I add a dummy field to import * as React from 'react'
interface Props {
unused?: number
}
function Component(_: Props) {
return <div>C2</div>
}
export function Bug() {
const props = { opt: "1" } as const
return <Component {...props} /> // <- Type '{ opt: "1"; }' has no properties in common with type 'IntrinsicAttributes & Props'
} |
|
Ok, I think my problem is not related to React/JSX but to interfaces, spread, and extra fields: function f(_: { foo: number }) {
return 1;
}
function g(props: { bar?: string }) {
f({ foo: 1 }) // TS: pass;
f({ foo: 1, bar: "1" }) // TS: error; due to extra 'bar' prop
f({ foo: 1, bar: "1", ...props }) // TS: pass; weird, I'd have expected a type error here!
f({ foo: 1, bar: "1", baz: "1", ...props }) // TS: error; because extra 'baz' field, but 'bar' still silently accepted.
} |
There are a lot of subtle things here but this isn't supposed to be one of them. |
Facing a similar issue. Passing spread props isn't get catch. I'm surprised this is happening in old versions |
My Playground with same error. type A = {
test: number
}
const a: A = {
test: 11,
// Where Error?
...{
sds: 23,
},
// OK: Error
sds: 23,
}
const b: A = {
test: 11,
// Where Error?
...{
sds: 23,
},
}
const c: A = {
test: 11,
// OK: Error
sds: 23,
}
|
Bug Report
π Search Terms
react, component props, interface type, spread
π Version & Regression Information
Bug present in 3.3.3 (oldest version available on Playground), as well as Nightly.
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
No error is indicated.
Error is indicated correctly when interface is changed to a type declaration, or the type is inlined.
π Expected behavior
TypeScript should indicate a type error since Component doesn't accept the 'opt' prop.
The text was updated successfully, but these errors were encountered: