【发布时间】:2022-07-21 20:03:46
【问题描述】:
我在 TS 文档中找到了此代码。 https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html#string-unions-in-types
“字符串&”是什么意思?我以为它只能用在接口上。
type PropEventSource<Type> = {
on(eventName: `${string & keyof Type}Changed`, callback: (newValue: any) => void): void;
};
【问题讨论】:
-
I thought it could only be used on interfaces.事实上,你可以写成string & number。它计算为never,但它确实编译了。
标签: typescript