首页 > 解决方案 > 泛型创建一个带有字符串和 trype 的对象

问题描述

我怎样才能MinObj按预期工作{ 'name': string }

type MinObj<Key extends string, Type> = { [a: Key]: Type }

type x = MinObj<'name', string>

游乐场链接

标签: typescriptgenerics

解决方案


您可以只使用映射类型Record,它完全符合您的要求:

type x = Record<'name', string>

游乐场链接


推荐阅读