Parsing a string to a literal type in TypeScript can be achieved by using a union of literal type...

I have a response from a server that will be in the structure of something like this

const MyData = Schema.Struct({
  type: Schema.String,
  input: Schema.Array(NameValue),
  output: Schema.Array(NameValue),
  _id: Schema.Number,
})
Schema.Struct({
  data: Schema.Array(MyData),
  id: Schema.String,
})


(obviously this is simplified).

MyData in this case, is really not "any string" it actually is Literal strings, like "Name", "Password", "TextInput", etc.

Is there a way to parse a string to a literal?

Basically I want to get the response from the server, and decode it so that my array of values is decoded into the actual schemas of each possible option. This Name value is really my discriminator here.

I'm sure this feels possible, just havent figured it out so figured I would ask.
Was this page helpful?