How do I specify an objects type when Key is a known value?

Ben Walters

Given the following code, how would I define the type of object data when data.type is a known value?

In the below example, I would like data to be typed accordingly when data.type is either "sms" or "email"

const payload = '{"type":"sms","destination":123}'

type PayloadType = 'sms' | 'email'

interface BasePayload {
    type: PayloadType
}

interface SmsPayload extends BasePayload {
    destination: number
}

interface EmailPayload extends BasePayload {
    destination: string
}

const data: SmsPayload | EmailPayload = JSON.parse(payload) 

TIA

*PS: I know phone numbers aren't numbers... this is just an example.

spender

It's hard to leave inheritance behind sometimes, but here it isn't necessary.

Just create two types with a common discriminating field, type:

interface SmsPayload {
    type: 'sms'
    destination: number
}

interface EmailPayload {
    type: 'email'
    destination: string
}

create a union type from these:

type PayloadUnion = EmailPayload | SmsPayload

now, you can:

const data: PayloadUnion = JSON.parse(payload) // trusting the backend here

and narrow using the common discriminator field

if (data.type === 'sms'){
    // in here `data` is of type SmsPayload
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I model a key/value for which the key is not known in Swagger

How do I get the child of a child when the key is not known in firebase

How do I specify both objects when overriding equals method?

How do I filter the unique objects from an array if the filter key can have any type of value?

How do I get specify value type from psql?

How do I specify a type that cannot be inferred when using if let?

Should I specify key-type and value-type when using ECache in Spring Boot?

How do I make a foreign key reference to a specific cell when the primary key is known in sql?

How to specify type of key, value pair in dictionary

How to define an interface when the type of a value depends on the key but not all keys are known in advance?

How do I extract fingerprints from .ssh/known_hosts? but filtering for a specific key type

Why do I get the error "the type of this value must be known in this context" when parsing a string to a number?

How to get value for key in array when the key is not known

How do I get the value of a key from a list of objects in Javascript?

How do I turn key value pairs in object into array of objects?

meteor:How do I iterate over an array of objects when property name is not known

How do I exclude null values when merging objects by key?

How do I specify the key file for sshfs?

In Bundle, how do I find out the type of a value for a given key?

How do I specify the compression type in zlib?

How do I specify the correct return type?

Do I need to specify a data type boolean in typescript when the value is already set in false?

How do I specify foreign_key and class when using has_many :through?

How do I convert "key=value, key=value, ..." to csv when some values contain the delimiter as part of the value?

Given an array of objects, how do I replace the key name with another whilst keeping the value from the original key?

How do I specify a type argument explicitly when reading an XML file in UWP

How do I specify more than one type to decode in the enum CodingKeys when using Codable?

Using jsdoc annotation, how do I specify the type to output of generic function when calling

How do I create and use a Stack of a type known at runtime