Typescript:基于兄弟对象键的值的强类型(和自动完成)

Typescript: Strong-typing (and autocomplete) for a value based on a sibling object's keys

假设我有一个看起来像这样的对象:

const configuration: Config = {
  options: {
    'Option 1': 'some value here',
    'Option 2': 'some other value here'
  },
  defaultOption: 'Option 1'
}

如何编写类型 Config 使得:

为了做到这一点并进行验证,您需要从函数参数推断您的配置对象:

type Configuration<
  Options
  >
  = {
    options: Options,
    defaultOption: keyof Options
  }

const config = <
  Options extends Record<string, string>,
  Config extends Configuration<Options>,
  Default extends keyof Config['options'],
  >(config: { options: Options, defaultOptions: Default }) => config

const result = config({
  options: {
    'Option 1': 'some value here',
    'Option 2': 'some other value here'
  },
  defaultOptions: 'Option 1'
}) // ok

const result2 = config({
  options: {
    'Option 1': 'some value here',
    'Option 2': 'some other value here'
  },
  defaultOptions: 'invalid property'
}) // error

Playground