Skip to content

Recommended way to type useQuery data if we know it cannot be undefined? #1493

Answered by phryneas
taylorkline asked this question in Q&A
Discussion options

You must be logged in to vote

Hmm. Something like

function assertHasData<T>(x: {data?: T}): asserts x is {data: T} {}


const result = useGetAccountQuery();
assertHasData(result)

// result.data is defined here

or

function assertIsDefined<T>(x: T|undefined): asserts x is T {}


const { data } = useGetAccountQuery();
assertIsDefined(data)

// data is defined here

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@taylorkline
Comment options

Answer selected by taylorkline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants