Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

typescript get object property by name

// Type
type UserType = {
	name: string;
    email: string;
};

// Object
const user: UserType = {
	name: "Bob",
    email: "bob@bob.com"
};

// Get Object property by name
const name = user["name" as keyof UserType] // name = "Bob"

// Alternatively, create object property type
type ObjectKey = keyof typeof user; // type ObjectKey = "name" | "email"

const myKey = "name" as ObjectKey;
const name = user[myKey]; // name = "Bob"
Comment

PREVIOUS NEXT
Code Example
Typescript :: how to send attachments to node mailer file not found 
Typescript :: typescript value in enum 
Typescript :: spyon observable 
Typescript :: typescript object key as enum 
Typescript :: how to add multiple arguments in discord commands rewrite 
Typescript :: typescript convert string to character array 
Typescript :: typescript get the promise return type 
Typescript :: react google charts x labels multiline 
Typescript :: serenity.is center open dialog 
Typescript :: createasyncthunk with typescript 
Typescript :: oracle objects in tablespace 
Typescript :: clean broken shortcuts in windows start menu 
Typescript :: return n elements from list java 
Typescript :: reverse mongo results order 
Typescript :: ts new example 
Typescript :: go Array’s length is part of its type. 
Typescript :: typescript foreach async await 
Typescript :: download toasts in django 
Typescript :: python get elements from list of dictionaries 
Typescript :: __REDUX_DEVTOOLS_EXTENSION_COMPOSE__ 
Typescript :: switch in typescript 
Typescript :: subscribe in angular 10 
Typescript :: how to list elements of an array C# 
Typescript :: paper menu rendered but not clickable 
Typescript :: minuts bwtewwn two date laravel 
Typescript :: IM DEAD 
Typescript :: useappdispatch 
Typescript :: When do you choose automated testing over manual testing? 
Typescript :: conditional statements in ti-82 
Typescript :: check if package exists inside the device adb 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =