TypeScript: Can't pass props with excess properties
TypeScript Version: 2.3.1
Code
With the above mentioned version I can’t spread Props objects anymore to child React components, when the Props don’t match up 100%.
/* App.tsx */
interface Props {
...
scopes: any;
}
interface State {}
export class App extends React.Component<Props, State> {
render() {
return <Content {...this.props} />; // error
}
}
/* Content.tsx */
interface Props {
...
// without scopes in Props
}
export default class Content extends React.Component<Props, {}>{
...
}
Expected behavior: No error and there wasn’t any on 2.2.1
Actual behavior:
error:
ERROR in ./spa/js/comps/App.tsx
(121,49): error TS2322: Type '{ ui?: UIState; session?: SessionState; scopes?: any; users?: any; roles?: any; plugins?: any; us...' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Content> & Props & { children?: ReactNode; }'.
Property 'scopes' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Content> & Props & { children?: ReactNode; }'.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 9
- Comments: 56 (19 by maintainers)
Commits related to this issue
- Fix #15463: use intersection types to emulate spread in generic react components — committed to microsoft/TypeScript by mhegazy 7 years ago
- Fix #15463: use intersection types to emulate spread in generic react components (#15851) * Fix #15463: use intersection types to emulate spread in generic react components * Fix lint errors *... — committed to microsoft/TypeScript by mhegazy 7 years ago
- Fix #15463: use intersection types to emulate spread in generic react components (#15851) * Fix #15463: use intersection types to emulate spread in generic react components * Fix lint errors *... — committed to microsoft/TypeScript by mhegazy 7 years ago
- [react-redux] Expose `dispatch` with props (#16652) — committed to DefinitelyTyped/DefinitelyTyped by blakeembrey 7 years ago
Honestly, I just started using Typescript in a React project, and I’ve spent most of my time dealing with issues like this. It’s a shame.
A little bit different but still the same.
now leads to this error
TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Panel> & Readonly<{ children?: ReactN...'. Type '{}' is not assignable to type 'void'.Had to change
voidtoany, null or undefinedto make it work.The same question. It’s hard to understand why the following code will not compile anymore in v2.3
due to the error
Btw, the same code without jsx will compile with no error
I’m also having this exact same issue and I’ve spent the better part of the day scouring the internet trying to find what’s wrong since this issue doesn’t have an update. I’ve tried following different tutorials on different ways to connect redux with react.
For the ~16 hours I’ve spent today on this I’m coming to the conclusion that React + Redux + TS doesn’t work.
Typescript: 2.7.2
Sample:
With this simple:
⬆️ One of a bunch of different ways I’ve tried.
I have the same problem as @levsthings, @mhegazy - the connect() method return type now seems to now require the props are set in the TSX when consuming the redux-connected component, whereas they are actually provided by
react-redux.UPDATE: This appears to be more of a problem with the latest react + react-redux typings, rather than typescript 2.3.4, since I get similar errors with TS 2.2.2
If anyone is able to decipher the issue with the typings please let me know! Heres a link… https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-redux/index.d.ts
For now I’m just throwing ‘as any’ after connect(…) 😄
Having a similar issue @
Typescript 2.3.4Type '{}' is not assignable to type 'Readonly<ISpinnerProps>'. Property 'showGlobalSpinner' is missing in type '{}Component 1:
Component 2:
This pattern to used to compile fine earlier.
Update: Please note that the example props in both of these components are passed via Redux.
@dupski @giladaya @levsthings Can you comment on the PR? I think that’s a better place for it than this issue since it’s a different cause.
This seems to be the breaking commit: https://github.com/DefinitelyTyped/DefinitelyTyped/commit/2bc7feeb48e5c79fac1d4a09b36b5ce8c80896af Works in types/react-redux@4.4.40 and not in types/react-redux@4.4.41
I’m also having issues with this
That should technically work as the common props should spread.
it should be in TS 2.3.3 later this week.
@luca-moser @luggage66 @etecture-plaube @pomadin The reason for change in behavior is that in 2.3,
JSXAttributesare type-check similar to object literal expression with freshness flag (this means that excess properties are not allowed) After discussing with @mhegazy, it is too restrictive and cases in examples here illustrate that. So we will make modification to the rule as follow:If there exist JSX attributes that are specified explicitly such as
<div classNames="hi" /> or<div {…this.props} classNames=“hi” />, attributes objects will get freshness flag and undergo excess properties hceckIf there are only spread in JSX attributes such as
<div {...this.props} />excess properties will be allowedI will get the PR up today and the fix should be in for tomorrow nightly if you would like to give that a try!
This issue is the reason I’m choosing Flow for my next project.
Started using v 2.6.1 today and can still see a lot of errors similar to ones discussed here, i.e.
/* errors /*-> i not assignable to type IntrinsincAttributes …this happens for all:
srcwidthanheight@levsthings this is also an issue when using mobx inject. Anyone have any insight on how to properly do this elegantly?
@giladaya You’re correct, I’ve also confirmed that the entire problem stems from
types/react-redux@4.4.41.@tsteuwer you should let
Bookto know that would be it’s props. that connected components will be exported, but not-connected-raw-Bookcomponent don’t know it’s type ofPropsso if you do this
then it will be ok
@mechanic22 It’s not perfect, but I just did
return React.createElement(MyList);to solve this.@ILL35T
NavScrollerextendsReact.Component<SportProps>so when rendering it you need to passSportPropsas its props, but you are not passing it any props and that is the reason for the error. If the props are optional then they need to be marked as such with ? or if you want to make them all optional then maybe extendReact.Component<Partial<SportProps>>@nileshgulia1
Property 'icon' is missing in type '{ label: "Force Update"; onClick: () => void; }'you need to either pass Button an ‘icon’ prop or mark it as optional on the ButtonProps interface.@yuit I have compiled my code via build 2.4.0-dev.20170509, the error is gone. So I am waiting for v2.3.3 to move on.
@luca-moser @luggage66 @pomadin @etecture-plaube @IIIristraM We have fix for original in the master. It should be out for nightly tonight. Give that a try and let us know. We will have this fix for 2.3.3 as well.
@IIIristraM Note: the fix doesn’t address " Spread types may only be created from object types" I will get that in another PR -> Update we have the fix into master nightly. Give it a try and let us know
I agreed with @luca-moser, it’s common case to extend the props or to override some ones.
@yuit, the final solution looks acceptable as the 1st case (only explicitly attrs…) fits no jsx case.
You can do like below, but may get a warning about scopes being unused.