是否可以指定字符串应与 TypeScript 中的接口 属性 名称匹配?

Is it possible to specify that a string should match an interface property name in TypeScript?

我有一个这样的界面,例如:

interface Account {
    email: string;
    enabled: boolean;
}

我想创建一个方法,returns 这些字段的默认值。所以我有这样的东西:

function defaultValue(propName:string) {
    if (propName === 'email') return '';
    if (propName === 'enabled') return true;
}

它运行良好,但是如果 Account 中的任何 属性 发生变化,defautValue() 方法仍然可以编译,即使它现在已损坏。

所以我想知道我是否可以指定 propName 应该有像 property name of Account 这样的类型?或者在那种情况下我可以使用任何其他好的模式来强​​制执行类型检查吗?

您可以使用一个函数来断言变量永远不会在 if 语句的末尾和 keyof T 获得键的并集:

function assertNever(v: never): never {
  throw new Error("Should never happen!");
}

function defaultValue(propName:keyof Account) {
    if (propName === 'email') return '';
    if (propName === 'enabled') return true;
    assertNever(propName);
}

Playground Link

您还可以通过使函数通用化来改善调用者体验,但这将意味着在实现中需要一些类型断言。


function defaultValue<K extends keyof Account>(propName:K): Account[K] {
    if (propName === 'email') return '' as Account[K];
    if (propName === 'enabled') return true as Account[K];
    assertNever(propName);
}

let email = defaultValue('email') // string
let enabled = defaultValue('enabled') // boolean

Playground Link

您可以通过向 propName 参数添加更具体的字符串类型来实现。然后,使用 switch 语句进行智能类型推断,即您正确地 return 为函数中的每个可能分支输入了与 return 类型匹配的值。

interface Account {
    email: string;
    enabled: boolean;
    shouldError: boolean;
}

function defaultValue(propName: keyof Account): Account[keyof Account] { // shows error for not always returning value
    switch (propName) {
        case "email":
            return "";
        case "enabled":
            return true;
        // uncomment this code to fix the error
        // case "shouldError":
        //     return true;
    }
}

TypeScript Playground Link