'How can I assert type of node in relay

I'm using react-relay and I want to fetch a node with following query.

node(id: $id) @required(action: THROW) {
  __typename
  id
  ...on user {
    name
  }
}

And the following code is for fetching.

const { node } = useLazyLoadQuery(query, { id })

When I checked the TypeScript type of node.name, it is string | undefined. It's because there is no guarantee that ... on user would match. However, in this case, I'm sure that the id given in the query's variable indicates a node of user type.

How can I assert this condition and make the type string not string | undefined?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source