admin管理员组

文章数量:1287575

I’m trying to translate some TypeScript type definitions into ArkType’s syntax, but I’m hitting a wall. Specifically, I’m trying to replicate TypeScript’s template literal types (ex application/${keyof typeof application}) in ArkType, but I’m not sure if I’m using the right approach or if ArkType even supports this kind of type composition.

Here’s what can do very elegantly in TypeScript:

const text = {
  html: ['html'],
  markdown: ['md'],
  xml: ['xml'],
  plain: ['txt', 'text', 'sh'],
  csv: ['csv'],
  css: ['css'],
  javascript: ['js'],
};

const video = {
  wmv: ['wmv'],
  mp4: ['mp4'],
  mpeg: ['mpeg'],
  matroska: ['mkv'],
};

const audio = {
  mp4: ['mp4'],
  mp3: ['mp3'],
  mpeg: ['mpeg'],
};

const image = {
  png: ['png'],
  jpeg: ['jpg', 'jpeg'],
  avif: ['avif'],
  AV1: ['avi'],
  webp: ['webp'],
};

const application = {
  json: ['json'],
  yaml: ['yaml', 'yml'],
};

export type ApplicationType = `application/${keyof typeof application}`;
export type ImageType = `image/${keyof typeof image}`;
export type VideoType = `video/${keyof typeof video}`;
export type AudioType = `audio/${keyof typeof audio}`;
export type TextType = `text/${keyof typeof text}`;
export type MimeType = ApplicationType | ImageType | VideoType | AudioType | TextType;

However I can't seem to translate this approach into the syntax ArkType will parse.

I've Naively tried this approach with a few different variations for the MediaTypes, but I am not having much success.

export const { MimeType } = type.module({
  // Subtype unions for each MediaType
  ApplicationSub: "'json' | 'yaml'",
  ImageSub: "'png' | 'jpeg' | 'avif' | 'AV1' | 'webp'",
  VideoSub: "'wmv' | 'mp4' | 'mpeg' | 'matroska'",
  AudioSub: "'mp4' | 'mp3' | 'mpeg'",
  TextSub: "'html' | 'markdown' | 'xml' | 'plain' | 'csv' | 'css' | 'javascript'",

  // MediaTypes with their extensions
  Application: "`application/${ApplicationSub}`", // <-- Type '"`application/${ApplicationSub}`"' is not assignable to type '"'`application/${ApplicationSub}`' is unresolvable "'.
  Image: "'image/'ImageSub", // <-- Type '"'image/'ImageSub"' is not assignable to type '"'I' is not allowed here "'.ts(2322)

  Video: "\'video/\'VideoSub", // <--Type '"'video/'VideoSub"' is not assignable to type '"'V' is not allowed here "'.
  Audio: "audio/${AudioSub}", // <-- Type '"audio/${AudioSub}"' is not assignable to type '"'audio/${AudioSub}' is unresolvable "'.
  Text: "text/${TextSub}",

  // Final union type
  MimeType: "Application | Image | Video | Audio | Text"
});

Can anyone point me in the right direction for expressing string template types with ArkType? or is not possible?

Thanks!

本文标签: stringHow to Translate TypeScript Template Literal Types into ArkType DefinitionsStack Overflow