Mobx-state-tree Types

Seoyul Kim·2021년 3월 9일
0

Mobx-State-tree

목록 보기
2/2

Types

complex types

  • type.model(properties, actions)

  • types.array(type): array type 을 지정할 때 사용된다.

  • types.map(type): map 형태로 데이터를 저장할 때 사용된다.

primitive types

  • types.string

  • types.number

  • types.integer

  • types.boolean

  • types.Date

  • types.custom : 자신만의 타입을 정의하여 사용하고자 할 때 사용한다.

utility types

  • types.union(options?: {dispatcher?: (snapshot) => Type, eager?: boolean}, types...)
    : 다양한 타입들을 결합하여 생성할 수 있다.snapshot에서 명확하게 알맞은 타입이 언급될 수 없다면 dispatcher function으로 타입을 결정하게 할 수 있다. eager가 true 라면(default) 첫번째로 매칭되는 타입이 사용되고 flase라면 단 한 타입만 매치하게 되면 타입체크를 통과하게된다.

  • types.optional(type, defualtValue, optionalValues?) : model에서 value를 optional로 설정하며 value가 없거나 undefined라면 defaultValue가 대신 사용된다. ID나 timestamp를 생성할 때 사용될 수 있다.

  • types.literal(value): 문자열 타입을 생성할 때 사용될 수 있으며 (특정한 value만이 가능하게) union과 함께 사용할 때 powerful 하다(ex: temperature: types.union(types.literal("hot"), types.literal("cold")))

  • types.enumeration(name?, options: string[]): string literal들의 union을 간단하게 사용할 수 있는 방법이다. 만약 타입스크립트를 사용하며 string enum 타입을 사용하고 싶다면 types.enumeration("color", Object.values(Color))로 사용할 수 있으며 color 인수는 optional이다.

  • types.refinement(name?, baseType, (snapshot) => boolean): 기본 타입보다 더 명확하게 타입을 지정하고 싶을 때 사용한다.(ex: types.refinement(types.string, vlaue => value.length>5))

  • types.maybe(type): optional하고 nullable 하게 타입 지정(ex: types.optional(types.union(type)))

  • types.maybeNull(type): maybe와 같지만 값이 없을 경우 null로 보여준다.

  • types.null

  • types.undefined

  • types.late(()=>type)

  • types.frozen(subType? | defaultValue)

    - types.frozen()
    - types.frozen(subType)
    - types.frozen(domDefaultValue)
    - types.frozen<TypeScriptType>()
  • types.compose(name?, type1...typeX)
  • types.reference(targetType)
  • types.safeReference(targetType)
  • types.snapshotProcessor(type, processors, name?)

property type

  • types.identifier: types.model 안에서 하나만이 존재할 수 있으며 object를 identify 할 수 있도록 unique 해야한다.

  • types.identifierNumber: types.identifier와 비슷하지만 serialization 하는 과정에서 identifier는 number로 parse 된다.

0개의 댓글