react: FunctionComponent and ComponentClass are not compatible with LibraryManagedAttributes
Annotating functions and classes with FunctionComponent and ComponentClass breaks LibraryManagedAttributes due to the static defaultProps property on those interfaces being set to optional.
import { FunctionComponent, ComponentClass, Component } from 'react'
export interface Props {
foo: string
bar?: boolean
}
const TestFunction: FunctionComponent<Props> = props => <div />
TestFunction.defaultProps = {
foo: '',
}
const TestClass: ComponentClass<Props> = class TestClass extends Component<Props> {
static defaultProps = {
foo: '',
}
render() {
return <div />
}
}
// type is never
type TestDefaultProps = typeof TestFunction extends { defaultProps: infer D } ? D : never
type TestClassDefaultProps = typeof TestClass extends { defaultProps: infer D } ? D : never
// type is Props because typeof Test does not extend { defaultProps } but rather { defaultProps? }
type TestManagedProps = JSX.LibraryManagedAttributes<typeof TestFunction, Props>
type TestClassManagedProps = JSX.LibraryManagedAttributes<typeof TestClass, Props>
This causes defaultProps to be completely ignored by JSX.LibraryManagedAttributes. We should probably remove it as a recommendation from the cheat sheet for now.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 19 (3 by maintainers)
Commits related to this issue
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to typescript-cheatsheets/react by swyxio 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to bernssolg/My-React-Sample by bernssolg 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to erinodev/My-React-project by erinodev 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to petardev101/react by petardev101 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to supercrytoking/react by supercrytoking 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to kevindavies8/react-full-stack-developer by kevindavies8 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to johnfrench3/react-Fronted-developer by deleted user 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to ericbrown2716/react-stack-build-website by ericbrown2716 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to peterjohnson4987/full-stack-developer-react by peterjohnson4987 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to renawolford6/react-husky-website by deleted user 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to Yoshidayoshi23/react by Yoshidayoshi23 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to renawolford6/react-dev-build-doc- by renawolford6 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to coopfeathy/cheatsheet by coopfeathy 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to dreamcoder75/react-sample by dreamcoder75 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to blackphantom0221/typescript-cheatsheets-react by blackphantom0221 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to tamatashi/chart by deleted user 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to spartacus0816/react_frontend_master by deleted user 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to AIDevMonster/Awesome-React by AIDevMonster 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to whiteghostDev/Awesome-React by whiteghostDev 5 years ago
- update React.FC and defaultProps recommendations update React.FC and defaultProps recommendations with reference to https://github.com/sw-yx/react-typescript-cheatsheet/issues/87 — committed to cedev935/React-TypeScript by cedev935 5 years ago
I was thinking more of something along these lines:
As that’s the pattern that is currently advised in the doc.
@sw-yx Well looks like you’re not the only one: Deprecate defaultProps on function components
Default values inside parameters (and when destructured) are better understood by the compiler. It will allow you to not have to hack together the props interface. The compiler knows it will always be defined inside the implementation but optional when consumed.
I wouldn’t do that either. I think it’s perfectly fine to explain the drawbacks of
explicit annotations on function expressions:
function declarations:
Personally I don’t like implicit children anyway. Not all of my components handle
children. I would hope typescript would recognize some day that<Foo>bar</Foo>is equivalent to<Foo children="bar" />and report an error on both call sites if the props ofFoodon’t define achildrenproperty.thanks - i’ll update accordingly. why is it considered a bad idea to have implicitly typed children? i thought that was a major benefit of using React.FC.
as a side note - i dont find myself ever using defaultProps for function components as i can assign when i destructure…
The linter error should be ignored or turned off, because destructuring and default values already achieves the same as
.defaultProps. The linter errors are just rules of thumb that are contextual, and this specific rule doesn’t apply in this case.If I could rewrite react types implicit children would be removed. It causes a lot more problems and was only placed there for convenience.
I always explicitly include children in my props interfaces anyway when I do support them.
i mean… destructure and assign defaults?