arktypea
arktype11mo ago
IOM

Inference on Mapped Types with Exhaustive Switch Possible?

Is it possible to get inference with an exhaustive switch with arktype using mapped types

For example, in the switch statement below im using mapped types to limit the possible type that can be inferred in each case. I want to try to get the type system to know that under each case only one possible type is possible so i want it to show me the correct fields i can select from

import { type } from 'arktype'

// Define the DTOs using ArkType
const dtos = {
  create: type({
    name: 'string',
    data: 'unknown'
  }),
  read: type({
    id: 'string'
  }),
  update: type({
    id: 'string',
    changes: 'object'
  }),
  delete: type({
    id: 'string',
    softDelete: 'boolean?'
  })
} as const

// Infer the operation type from the DTO keys
type Operation = keyof typeof dtos

// Infer the DTO types from ArkType
type DTOMap = {
  [K in Operation]: (typeof dtos)[K]['infer']
}

function performOperation<T extends Operation>(
  op: T,
  dto: DTOMap[T]
) {
  switch (op) {
    case 'create':
        console.log(dto.) <--- how do i get arktype to infer that only the fields relevant to the create dto should be relevant here

    case 'read':

    case 'update':
  
    case 'delete':

    default:

  }
}
image.png
Was this page helpful?