我不知道如何正确地表达我的问题,所以我举个例子。
type ValueType = "NUM" | "STR";
type TypeOf<T>
= T extends "NUM" ? number
: T extends "STR" ? string
: never;
interface TypedValue<T = ValueType> {
type: T;
data: TypeOf<T>;
}
// Compiles, as intended
const test1: TypedValue = { type: "NUM", data: 123 };
// Does not compile, as intended
const test2: TypedValue<"NUM"> = { type: "NUM", data: "123" };
// Should not compile, but does...
const test3: TypedValue = { type: "NUM", data: "123" };
Run Code Online (Sandbox Code Playgroud)
似乎 …
我正在制作一个命令行 nodejs 工具,它使用 Typescript 语言服务自动重命名 Typescript 文件中的符号。
您告诉工具:将此类型的所有符号重命名为此符号。就像 resharper 一样,它也会重命名局部变量、属性等。由于它允许一次重命名多个符号,因此您还可以交换两个符号名称,而不需要中间临时唯一名称(例如,将 Foo 重命名为 Bar,反之亦然)。
我必须将语言服务中的私有函数 getSymbolInfoAtPosition 公开才能使其工作,以便我可以获得 PullSymbol 信息
目前,它仅通过在 PullSymbol 上调用 getNameAndTypeName 来检测精确的名称+类型匹配,但我想执行结构兼容的匹配。
在 C# 中,这很容易,因为 Type 有一个 IsAssignableFrom 方法。
有谁知道如何使用 Typescript 编译器即服务来检测一个 PullSymbol 是否在结构上与另一个 PullSymbol 兼容?
非常感谢,彼得·韦斯维伦
rename automated-refactoring languageservice typescript typescript-compiler-api