Typescript Function to make specific nested keys non nullable?
Does anyone know of a Typescript Function to make a bunch of provided nested keys non-nullable? Sometimes I want to reuse a GQL type but want to remove a bunch of nulls/undefined from nested keys.
E.g.
I came up with this so far but I wouldn't be surprised if someone already knows of a better solution: https://www.typescriptlang.org/play?#code/C4TwDgpgBA4hwCUD2TgAUCGwAWAeAKgHxQC8U+UEAHsBAHYAmAzlAAYAkA3gJZ0BmEAE5RkqAL4A6Lk2CDeAczGsoAfhEpgUAFxQ6EAG5CA3AChQkWPAByEGRAaYcAdW45RwXCajkANF-WolDT0zFAycnTyJsRkFNS0jCwcnO6SXLwCwjZ2DEqqUNm0DNq6BsYmJgD0lQUQ9lDASFDcALZggkiGzZpMTa5Q2BiMADa2UEhgwNxIdBjDUADWECBMKlU1TkgArsPFAEbQ8oIQWM18UI7YLPEhLIX2lyy8dhjFSOfhCuu19Y1Qxy1OtAWhAWgdBCx3hcsNgGoNNBhjlAMFAAERMLZ7AC0YBhqIkUAAohJ5ATuOdLqQ0RhUVAAD7UiR7WncFjtTrcBj2Hz-UFA6moszgaBWGZWHbDDB7UYAIRAlwIPMpN0SYVkChiUE4-gA2gBpZp0RbLKH4AC6OgNKtCl383jUoro4uGkulEAI+rNhDtJStwVVcEQGgVl293ntBTFEqlsvlMNwjudrtGHr1Xp5gfuDhhLjcGlwlx5esIYfDOnwntMYlMQosAFUmEIqdrvJydHQtmCmwyOy76VAtowIHxePZTN5XgxjkwmFofQyW+Gl4PXOKu4IdJ9Iv3e8Nx0vw+E6sA1+DN+rtz2JfuD2rjvBzxF5Dvrz6lyDgK8sBgdIvb949ERTBBAWHQ9hQUYhhfF0bwPMRoL3N8oDEeddAlVDBy5Ec9AYKsa3MaAADU5k5LB7AbJsyETaM3TlBV-AowQ-G8VFOVpBlUUnacmAkI9rE7cF2OpBgp1sHi+OAITOJE7iJA-L9P0FQhTCAA
Also we need a Typescript tag 😉
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
7 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Awesome stuff !
I'll probably wrap
NonNullableByPath
with an Omit<..., never>
to merge the intersection into a single type for a slightly easier to read intellisense hover tooltip
Need to remove members of Path that are a "sub-path". E.g. if Path = "a" | "a.b" is provided, remove "a"I was thinking about this scenario, where
address
is redundant. But if it's not filtered out from the Paths
union, it results in a type where address.metadata
can still be null.
Trying to come up with a typescript function to filter out "subpaths" now. It seems like it should be easy but I'm getting stuck with the peculiarities with how unions are distributed in a condition.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Wow thanks, really appreciate breaking it down for me like that. I think I understand it a lot more now. I was not expecting
never
to extend everything else but now that I think about it, it makes sense given that never
is an empty set. So of course an empty set is a subset of all other sets.
I actually wanted GetUniquePaths
to filter out the most "shallow" path and retain the deeper paths. So I wanted this output:
Thanks to your explanation, I ended up implementing it like this:
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View