Get `__typename` from interface
Asked Answered
C

0

6

I have a ts interface (for an apollo-client@v2 gql query) that has a __typename property. In the existing codebase there is a lot of "rehydration" of data object that re-adds the __typename type of interfaces in a "magic string" way. I am wondering if there is a way to reference a string type via ts or a way to avoid doing this via the existing type or cache systems.

An example (generated) interface:

export interface SomeNode {
  __typename: "SomeNode";
  id: string;
  name: string;
}

Is there a way to get the "SomeNode" as a value and as a type?

const newObject: SomeNode = {
  __typename: string(NewNode.__typename) as NewNode.__typename,
  id: 'some value',
  name: 'some name'
}

Currently we do this:

const newObject: SomeNode = {
  __typename: "NewNode" as "NewNode",
  id: 'some value',
  name: 'some name'
}

This is a simplified example and the usage does seem weird. But I am looking for a way to do this specific thing. Without this, we end up recreating the already generated types throughout our codebase.

Complainant answered 16/10, 2020 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.