프로그래밍 언어/JavaScript & TypeScript

[TypeScript] 매번 다르게 전달되는 JSON 에 대해 타입 정의하기

DevPing9_ 2022. 9. 17. 17:18
interface Person {
  [key: string]: string | number;
  name: string;
  age: number;
  userIdentity: { [key: string]: string };
}


const p1: Person = { name: 'Tom',
                     country: 'Chile',
                     age: 30,
                     userIdentity: {userName: 'Ping9', region: 'us-east-1'}};

 

 

Reference

 

What does {[key: string]: string} mean in TypeScript | bobbyhadz

The `{[key: string]: string}` syntax is an index signature in TypeScript and is used when we don't know all the names of a type's properties ahead of time, but we know the shape of the values. The index signature specifies a key and value of type `string`.

bobbyhadz.com

 

 

728x90