简体   繁体   English

你如何使用 Typescript 编译器的 Typechecker 来获取声明的(别名)类型,而不是解析的类型?

[英]How do you use the Typescript compiler's Typechecker to get the declared (alias) type, rather than the resolved type?

I realize this is somewhat obscure, but maybe someone else has run into this or knows the Typescript compiler well.我意识到这有点晦涩,但也许其他人已经遇到过这个问题或者很了解 Typescript 编译器。 I am processing Typescript files using Typescript's compiler API, based on examples like this: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API我正在使用 Typescript 的编译器 API 处理 Typescript 文件,基于这样的例子: https : //github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API

Imagine I have a declared function like this in Typescript:想象一下,我在 Typescript 中有一个这样的声明函数:

export type DateString = string;
export function parseDate(date: DateString): Date{
    let parsedDate = Date.parse(date);
    let retVal = new Date();
    retVal.setTime(parsedDate);
    return retVal;
}

In the examples linked above, you can see methods like this defined to extract information about symbols:在上面链接的示例中,您可以看到这样定义的方法来提取有关符号的信息:

function serializeSymbol(symbol: ts.Symbol): DocEntry {
    return {
        name: symbol.getName(),
        type: checker.typeToString(checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration))
    };
}

When you run checker.typeToString(checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration) on the date: DateString symbol, instead of returning DateString , it returns string . In other words, you don't get the declared type alias, but the fully resolved type. In my case, I'd like to know that the type of the date field is DateString . Is there an easy way to look up a parameter's declared type rather than its resolved type?当您在date: DateString上运行checker.typeToString(checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration)date: DateString符号,而不是返回DateString ,它返回string 。换句话说,您没有得到声明的类型别名,而是完全解析类型。在我的情况下,我想知道date字段的类型是DateString 。有没有一种简单的方法来查找参数的声明类型而不是其解析类型?

Unfortunately this doesn't work because of "type interning."不幸的是,由于“类型实习”,这不起作用。 See here .这里

What works is to get the text of the typeNode .有效的是获取typeNode的文本。 So basically get the text from the node.所以基本上从节点获取文本。 Here's a working example that you may be apply to your scenario:这是您可能适用于您的场景的工作示例:

// fileToAnalyze.ts
type DateString = string;
function myFunction(date: DateString) {
}

// main.ts
import * as ts from "typescript";
import * as path from "path";

const program = ts.createProgram([path.join(__dirname, "fileToAnalyze.ts")], { });
const file = program.getSourceFiles().filter(f => /fileToAnalyze/.test(f.fileName))[0];

const funcNode = (file.statements[1] as ts.FunctionDeclaration);
const parameterNode = funcNode.parameters[0];
console.log(parameterNode.type.getText(file)); // DateString

By the way, you might want to check out this library ts-type-info ts-simple-ast ts-morph that I've been working on in case you haven't seen it.顺便说一句,您可能想查看我一直在研究的这个库 ts-type-in​​fo ts-simple-ast ts-morph ,以防您没有看到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在打字稿中获取属性的声明类型而不是赋值的类型 - Is there a way to get the declared type of a property rather than the type of the assigned value in typescript 如何从TypeScript编译器API中的TypeReferenceType获取声明的类型? - How to I get the declared type from a TypeReferenceType in TypeScript Compiler API? Vscode Typescript:显示类型的别名,而不是其定义 - Vscode Typescript: Display alias of type rather than its definition 使用 TypeScript Compiler API 从类型引用节点获取类型别名声明节点 - Use TypeScript Compiler API to get the Type Alias Declaration Node from a Type Reference Node 通过在 Typescript 接口中的属性上声明的 key 获取泛型值的类型,并将其用作函数参数的类型 - Get type of generic's value by key declared on a property in Typescript interface and use it as the type of a function's parameter TypeScript编译器API:访问已解决的“此”参数类型 - TypeScript Compiler API: Accessing resolved type of 'this' parameter TypeChecker API:如何找到推断类型 arguments 到 function? - TypeChecker API: How do I find inferred type arguments to a function? TypeScript类型检查类型而不是实例 - TypeScript type checking on type rather than instance 在 TypeScript 中,我如何使用索引类型查询运算符来仅获取类型属性的子集? - In TypeScript how do I use index type query operator to get only subset of type's properties? TypeScript:如何指定内联对象的类型? - TypeScript: How do you specify an inlined object's type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM