gpt4 book ai didi

typescript - 如何从 TypeScript Compiler API 中的 TypeReferenceType 获取声明的类型?

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:20 24 4
gpt4 key购买 nike

我目前正在使用 TypeScript 2.5.3 Compiler API。如果有帮助,我可以更新到 2.6.x。我想弄清楚如何获取父类的 TypeNode 。我以为我可以使用 TypeChecker,但还没弄明白。以下是 Mocha index.d.ts 中的一些相关部分:

    export class Base {
constructor(runner: IRunner);
}

export class Doc extends Base { }

在阅读 ClassDeclaration 时,我可以访问它的 HeritageClause。每个 HeritageClause 都是一个 ExpressionWithTypeArguments,它是一个 TypeReferenceType。如何获取 BaseClassDeclaration

这是给 https://github.com/fable-compiler/ts2fable/issues/83

2017-11-17 周五更新,这里是 TypeScript 示例。这输出:

heritage clause: extends Base
contextual type: undefined
import * as ts from "typescript";
import * as fs from "fs";

const tsPath = "node_modules/@types/mocha/index.d.ts"
const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES2015 }
const host = ts.createCompilerHost(options, true);
const program = ts.createProgram([tsPath], options, host)
const checker = program.getTypeChecker()
const sourceFile = program.getSourceFile(tsPath)
// console.log(sourceFile)

function visitNode(node: ts.Node){
switch (node.kind) {
case ts.SyntaxKind.ClassDeclaration:
const cd = (<ts.ClassDeclaration>node);
// console.log(cd.name.getText())
if(cd.name.getText() === "Doc"){
printBaseClass(cd)
}
}
ts.forEachChild(node, visitNode)
}

ts.forEachChild(sourceFile, visitNode);

function printBaseClass(cd: ts.ClassDeclaration){
// console.log(cd)
for(const hc of cd.heritageClauses) {
console.log("heritage clause: " + hc.getText())
for(const hctp of hc.types){
// hctp is a ts.ExpressionWithTypeArguments

const ct = checker.getContextualType(hctp.expression)
console.log("contextual type: " + ct) // undefined
}

}
}

最佳答案

我是 tsutils 的作者库向我展示了如何使用它的 isTypeReferencetp.target.symbol.declarations 来获取我正在寻找的类声明。这是在TypeScript issue .

import * as ts from "typescript";
import * as fs from "fs";
import {isTypeReference, isClassDeclaration} from "tsutils";

const tsPath = "node_modules/@types/mocha/index.d.ts"
const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES2015 }
const host = ts.createCompilerHost(options, true);
const program = ts.createProgram([tsPath], options, host)
const checker = program.getTypeChecker()
const sourceFile = program.getSourceFile(tsPath)
// console.log(sourceFile)

function visitNode(node: ts.Node){
switch (node.kind) {
case ts.SyntaxKind.ClassDeclaration:
const cd = (<ts.ClassDeclaration>node);
// console.log(cd.name.getText())
if(cd.name.getText() === "Doc"){
printBaseClass(cd)
}
}
ts.forEachChild(node, visitNode)
}

ts.forEachChild(sourceFile, visitNode);

function getFullTypeName (tp: ts.Type){
if(tp.symbol){
return checker.getFullyQualifiedName(tp.symbol)
} else {
return ""
}
}

function getFullNodeName (nd: ts.Node){
const tp =checker.getTypeAtLocation(nd)
return getFullTypeName(tp)
}

function printBaseClass(cd: ts.ClassDeclaration){
// console.log(cd)
for(const hc of cd.heritageClauses) {
console.log("heritage clause: " + hc.getText())
console.log("hc name " + getFullNodeName(hc))
for(const hctp of hc.types){
let tp = checker.getTypeFromTypeNode(hctp)
console.log(getFullTypeName(tp))

if (isTypeReference(tp)) {
let dcs = tp.target.symbol.declarations
console.log(dcs.length + " declarations")
for(const dc of dcs){
console.log(getFullNodeName(dc))
if(isClassDeclaration(dc)){
for(const mb of dc.members){
console.log(" member " + mb.getText())
}
}
}
}
}
}
}

关于typescript - 如何从 TypeScript Compiler API 中的 TypeReferenceType 获取声明的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47340859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com