TypeScript problem with iterating over object properties

TypeScript problem with iterating over object properties

1. Problem

For example, the following code:

type Animal = {
    name: string;
    age: number
}

const animal:Animal={
    name:"dog",
    age:12
}

function test(obj:Animal) {
    for (let k in obj) {
        console.log(obj[k]). //Error here}
}
test(animal)

Error:

2. Solution

1. Declare the object as any

function test(obj:Animal) {
    for (let k in obj) {
        console.log((obj as any)[k]) //No error}
}


This method directly bypasses the typescript verification mechanism

2. Declare an interface for the object

type Animal = {
    name: string;
    age: number;
    [key: string]: any
}

const animal:Animal={
    name:"dog",
    age:12
}

function test(obj:Animal) {
    for (let k in obj) {
        console.log(obj [k]) //No error}
}
test(animal)

This can be used for more common object types, especially some tool methods.

3. Use Generics

function test<T extends object>(obj:T) {
    for (let k in obj) {
        console.log(obj [k]) //No error}
}

4. Use keyof

function test(obj:Animal) {
    let k: (keyofAnimal);
    for (k in obj) {
        console.log(obj [k]) //No error}
}

This is the end of this article about TypeScript traversing object properties. For more information about TypeScript traversing object properties, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • TypeScript enumeration basics and examples
  • In-depth reading and practice records of conditional types in TypeScript
  • Typescript+react to achieve simple drag and drop effects on mobile and PC

<<:  Table setting background image cannot be 100% displayed solution

>>:  CSS realizes the scene analysis of semi-transparent border and multiple border

Recommend

About the selection of time date type and string type in MySQL

Table of contents 1. Usage of DATETIME and TIMEST...

Tips for List Building for Website Maintenance Pages

And, many times, maintenance requires your website...

JS implements multiple tab switching carousel

Carousel animation can improve the appearance and...

Sharing tips on using scroll bars in HTML

Today, when we were learning about the Niu Nan new...

Linux file system operation implementation

This reading note mainly records the operations r...

Detailed explanation of the EXPLAIN command and its usage in MySQL

1. Scenario description: My colleague taught me h...

What is html file? How to open html file

HTML stands for Hypertext Markup Language. Nowada...

Zabbix monitors Linux hosts based on snmp

Preface: The Linux host is relatively easy to han...

Detailed installation process and basic usage of MySQL under Windows

Table of contents 1. Download MySQL 2. Install My...

Summary of important components of MySQL InnoDB

Innodb includes the following components 1. innod...

Several ways to clear arrays in Vue (summary)

Table of contents 1. Introduction 2. Several ways...

Use CSS to draw a file upload pattern

As shown below, if it were you, how would you ach...

MySQL advanced learning index advantages and disadvantages and rules of use

1. Advantages and Disadvantages of Indexes Advant...

Process parsing of reserved word instructions in Dockerfile

Table of contents 1. What is Dockerfile? 2. Analy...