Recent typescript upgrade resulted in state subfields possibly being undefined
See original GitHub issueHi!
So I went back to MoonPiano and upgraded a bunch of dependencies, most importantly:
- "@hookstate/core": "^1.4.1",
- "@hookstate/persistence": "^1.2.2",
+ "@hookstate/core": "^1.7.0",
+ "@hookstate/persistence": "^1.5.2",
- "npm": "^6.13.6",
+ "npm": "^6.14.2",
- "react": "^16.12.0",
+ "react": "^16.13.0",
- "react-dom": "^16.12.0",
+ "react-dom": "^16.13.0",
- "typescript": "^3.7.4",
- "webpack": "^4.41.5",
- "webpack-cli": "^3.3.10"
+ "typescript": "^3.8.3",
+ "webpack": "^4.42.0",
+ "webpack-cli": "^3.3.11"
And then, whenever I access nested field of any state, typescripts adds the possiblity of it being defined, i.e:
assuming user is a StateLink with field friends: UserFriends[].
The following code no longer works:
const friends = user.nested.friends.get().map(e => e.id)
Typescript now assumes user.nested.friends is of type friends: StateLink<UserFriendInterface[] | undefined>
Thus in order for it to work, I have to manually cast it to:
const friends = (user.nested.friends as StateLink<UserFriendInterface[]>).get().map(e => e.id)
I am not saying the problem is with hookstate, but I could really use some help on how to solve this without major changes, because I use HookState all over the place! The number of times i access the nested field of a state is 267 in a total of 33 files 😅
Cheers!
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Object is possibly 'undefined'. after upgrading from typescript ...
I'm using typescript on a react project and I just upgraded from Typescript 3.7.2 to 3.8. Now I'm getting an Object is possibly...
Read more >How to solve TypeScript possibly undefined value
This article goes over 5 methods to solve a problem like TS2532: Object is possibly 'undefined'. You can use IF conditions, logical operator ......
Read more >Object is possibly 'undefined' error in TypeScript | bobbyhadz
The "Object is possibly 'undefined'" error occurs when we try to access a property on an object that may be undefined (e.g. marked...
Read more >Compiler complains object is possibly undefined after ... - GitHub
TypeScript Version: 3.5.0-dev.20190420 Search Terms: Object is possibly 'undefined' Code ... undefined) throw new Error("One of the params must be provided.
Read more >Customizing the behavior of cached fields - Apollo GraphQL
You can customize how a particular field in your Apollo Client cache is read and written. To do so, you define a field...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

No worries. Glad it helped. I am not sure about the config, but I think the difference is in the strict mode.
BTW. Look at the StateLink.denull() function, it may help you in some other cases where undefined is still a legitimate case.
On Fri, Mar 13, 2020 at 1:39 PM Soulaymen Chouri notifications@github.com wrote:
Regarding the injected undefined in the nested state link. I can not reproduce it. I have created the sample here https://github.com/avkonst/hookstate/blob/master/hookstate.js.org/demos/exp1/src/App.tsx
Nothing has changed for nested in recent releases. So not sure. Can it be inferred by something else? Could you double check the type of the parent link? Will the following compile in your code?