admin管理员组文章数量:1399313
I am using Typescript in a react project which includes React Router (v6).
React Router returns a location object from the useLocation() hook
const location = useLocation()
which has a property called "state". State is an object which can be passed to any url and can have any properties you want. It simply has a type of "Object" because the user can set any number of properties on the state.
The trouble es when I e to access a property which may or not be part of the state. My JS (non-typescript) code had this...
if(location.state && location.state.modals === true){...do something}
But the second part of the expression gives me an error... Property 'modals' does not exist on type 'object'
Now, I know that a generic object (which is the type of the state, set by the library author, not me) doesn't have a property called .modals but I can't do a lot about that, it could have literally any propery depending on what page of the app we're currently in and what's contained in the state at that moment.
I'm at a bit of a loss about how to fix it.
I am using Typescript in a react project which includes React Router (v6).
React Router returns a location object from the useLocation() hook
const location = useLocation()
which has a property called "state". State is an object which can be passed to any url and can have any properties you want. It simply has a type of "Object" because the user can set any number of properties on the state.
The trouble es when I e to access a property which may or not be part of the state. My JS (non-typescript) code had this...
if(location.state && location.state.modals === true){...do something}
But the second part of the expression gives me an error... Property 'modals' does not exist on type 'object'
Now, I know that a generic object (which is the type of the state, set by the library author, not me) doesn't have a property called .modals but I can't do a lot about that, it could have literally any propery depending on what page of the app we're currently in and what's contained in the state at that moment.
I'm at a bit of a loss about how to fix it.
Share Improve this question edited Feb 2, 2022 at 11:35 Ben Smith 20.3k6 gold badges73 silver badges97 bronze badges asked May 16, 2020 at 23:14 jonhobbsjonhobbs 28k39 gold badges118 silver badges179 bronze badges2 Answers
Reset to default 6One way to get around this problem is to use type assertion via the "as" keyword to tell the piler to consider the state object as any type e.g.
if(location.state && (location.state as any).modals){...do something}
This would allow state to have any value but we both know that it would be better to deal with a more rigidly defined type!
The React Router types allow you to specify what state you expect on your location object:
https://github./DefinitelyTyped/DefinitelyTyped/blob/843feeed9961898fc2d73dc80dba3502f6608386/types/react-router/index.d.ts#L163
export function useLocation<S = H.LocationState>(): H.Location<S>;
Therefore I believe you should be able to fix this error by defining your location as follows:
const location = useLocation<{ modals: string }>()
版权声明:本文标题:javascript - Property 'xxx' does not exist on type 'object' when object is of unknown type - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744195325a2594713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论