简体   繁体   English

如何在 Typescript 的接口中将联合(或其他)类型设置为强制键名

[英]How to set union(or otherelse) type as mandatory key name in interface in Typescript

I want to union type as enum of keys for some interface for snippeting in vscode.我想联合类型作为一些接口的键枚举,以便在 vscode 中进行片段化。 Assume that some union types,假设一些联合类型,

// myunion.d.ts
declare module 'some_module' {
  export type TargetDirectory = 'action'|'nav'|'content'
}

And make predefined interface for build preset并为构建预设制作预定义界面

// preset.d.ts
import 'some_module'
declare module 'some_module' {
export interface BuildPreset {
    ...,
    predefinedTarget: { [ key: TargetDirectory]: string } // Seems not work with this code, No snippet error
  }
}

If I declare some variable with BuildPreset interface, in predefinedTarget field, all values 'action', 'nav', 'content' should exists and no other values should not.如果我用BuildPreset接口声明了一些变量,在predefinedTarget的目标字段中,所有值 'action'、'nav'、'content' 都应该存在,并且没有其他值不应该存在。 It should be like below,它应该像下面,

const inter1: BuildPreset = { // This is Okay
  ...,
  predefinedTarget: {
    'action': 'a/b/c/',
    'nav': 'e/f/g',
    'content': 'h/i/j',
  }
}
const inter2: BuildPreset = { // key 'action' is missing -- ERROR
  ...,
  predefinedTarget: {
    'nav': 'A/B/C/',
    'content': 'D/E/F',
  }
}
const inter3: BuildPreset = { // Key 'other' is not possible - ERROR
  ...,
  predefinedTarget: {
    'action': 'a/b/c/',
    'nav': 'e/f/g',
    'content': 'h/i/j',
    'other': 'k/l/m/'
  }
}

I have lots of possible values of TargetDirectory , so code snippet should check it for preventing mistake.我有很多TargetDirectory可能的值,所以代码片段应该检查它以防止错误。 How can I make this possible modifying both myunion.d.ts and preset.d.ts ?如何使这成为可能同时修改myunion.d.tspreset.d.ts I think I need to totally modify both d.ts file.我想我需要完全修改这两个d.ts文件。 Please help me.请帮我。

For unions, The syntax is [key in TargetDirectory] .对于联合,语法是[key in TargetDirectory] You are writing it as [key: TargetDirectory] , which is incorrect.您将其写为[key: TargetDirectory] ,这是不正确的。

The syntax [key: ...] is to specify what primitive type the key should be, such as string , number , or symbol .语法[key: ...]用于指定 key 应该是什么原始类型,例如stringnumbersymbol

type TargetDirectory = 'action' | 'nav' | 'content';

interface BuildPreset {
  predefinedTarget: {
    [key in TargetDirectory]: string;
  }
}

const inter1: BuildPreset = { // This is Okay
  predefinedTarget: {
    'action': 'a/b/c/',
    'nav': 'e/f/g',
    'content': 'h/i/j',
  }
}
const inter2: BuildPreset = { // key 'action' is missing -- ERROR
  predefinedTarget: {
    'nav': 'A/B/C/',
    'content': 'D/E/F',
  }
}
const inter3: BuildPreset = { // Key 'other' is not possible - ERROR
  predefinedTarget: {
    'action': 'a/b/c/',
    'nav': 'e/f/g',
    'content': 'h/i/j',
    'other': 'k/l/m/'
  }
}

Demo in TypeScript playground . TypeScript 操场上的演示

You may find more information here in the docs .您可以在文档中找到更多信息。

So no, you don't need to completely modify 2 files.所以不,您不需要完全修改 2 个文件。 Only 1 line in preset.d.ts will do. preset.d.ts中只有 1 行可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM