여기에는 테스트용 모의 GraphQL 객체 역할을 하는 fakeDataDefaults 라는 객체가 있습니다.

export const fakeDataDefaults = {
  String: "Default string",
  Int: 1,
  Float: 1.14,
  Boolean: true,
  ID: "id",
}

fakeDataDefaults 위로 마우스를 가져가면 각 필드의 타입을 확인할 수 있습니다.

// Hover over fakeDataDefaults
const fakeDataDefaults: {
  String: string;
  Int: number;
  Float: number;
  Boolean: boolean;
  ID: string;
}

Challenge

여러분의 과제는 fakeDataDefaults 를 타입으로 변환한 다음 각 객체를 개별 조각으로 추출하는 것입니다.

export type StringType = unknown
export type IntType = unknown
export type FloatType = unknown
export type BooleanType = unknown
export type IDType = unknown

이 시나리오에 유용한 도구를 찾으려면 TypeScript 문서를 참조하세요.

나의 답

import { Equal, Expect } from '../helpers/type-utils';

export const fakeDataDefaults = {
  String: 'Default string',
  Int: 1,
  Float: 1.14,
  Boolean: true,
  ID: 'id',
};

export type StringType = typeof fakeDataDefaults['String'];
export type IntType = typeof fakeDataDefaults['Int'];
export type FloatType = typeof fakeDataDefaults['Float'];
export type BooleanType = typeof fakeDataDefaults['Boolean'];
export type IDType = typeof fakeDataDefaults['ID'];

type tests = [
  Expect<Equal<StringType, string>>,
  Expect<Equal<IntType, number>>,
  Expect<Equal<FloatType, number>>,
  Expect<Equal<BooleanType, boolean>>,
  Expect<Equal<IDType, string>>
];

Indexed 액세스 타입을 사용하여 객체 속성 추출

해결책은 인덱싱된 액세스 타입을 사용하는 것입니다.

type FakeDataDefaults = typeof fakeDataDefaults