Javascript Typescript源解析:获取与特定react类相关的接口
我想从与该类相关的接口中声明的React类中获取道具。 例如,我有这种代码Javascript Typescript源解析:获取与特定react类相关的接口,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我想从与该类相关的接口中声明的React类中获取道具。 例如,我有这种代码 interface SomeProps { text: string; label?: string; } class SomeClass extends React.Component<SomeProps> { ... } interface SomeProps{ 文本:字符串; 标签?:字符串; } 类SomeClass扩展了React.Component{ ... } 如何使用
interface SomeProps {
text: string;
label?: string;
}
class SomeClass extends React.Component<SomeProps> {
...
}
interface SomeProps{
文本:字符串;
标签?:字符串;
}
类SomeClass扩展了React.Component{
...
}
如何使用ts编译器api从源文件获取SomeProps中声明的SomeClass的props 确保可以使用编译器API来实现这一点
// sample.ts
import * as React from 'react'
interface SomeProps {
text: string;
label?: string;
}
class SomeClass extends React.Component<SomeProps> {
}
// getProps.ts
import * as ts from "typescript";
function isReactComponent (baseType: ts.Type) {
if (baseType == null) return false;
let baseTypeSymbol = baseType.getSymbol();
if (baseTypeSymbol == null) return false;
// Base type is named Component
if (baseTypeSymbol.getName() !== "Component") return false;
var declarations = baseTypeSymbol.declarations;
if (declarations == null) return false;
// With the declartion beeing located inside the react module
return declarations.some(r => r.getSourceFile().fileName.indexOf("node_modules/@types/react") !== -1);
}
function compile(fileNames: string[], options: ts.CompilerOptions): void {
let program = ts.createProgram(fileNames, options);
let sample = program.getSourceFile("sample.ts");
if (sample == null) return;
let checker = program.getTypeChecker();
// Get declarations inside the file
let list = sample.getChildAt(0) as ts.SyntaxList;
for (let i = 0, n = list.getChildCount(); i < n; i++) {
let clazz = list.getChildAt(i);
// if the child is a class
if (!ts.isClassDeclaration(clazz)) continue;
// Get the heritage classes of the class
let heritageClauses = clazz.heritageClauses;
if (heritageClauses == null) continue;
// Iterate the heritage clauses
for (const heritageClause of heritageClauses) {
// Only take the extends clauses
if (heritageClause.token !== ts.SyntaxKind.ExtendsKeyword) continue;
// Get the type of the extends
let extendsType = heritageClause.types[0];
// If the base type is React.Component
if (isReactComponent(checker.getTypeFromTypeNode(extendsType))) {
// Get the type argument of the expression
var propType = extendsType.typeArguments![0];
let type = checker.getTypeFromTypeNode(propType);
console.log(`The name of props is ${type.getSymbol()!.getName()}`);
var props = type.getProperties();
for (let prop of props) {
console.log(` Contains prop: ${prop.getName()}`);
}
}
}
}
}
compile(["sample.ts"], {});
//sample.ts
从“React”导入*作为React
介面道具{
文本:字符串;
标签?:字符串;
}
类SomeClass扩展了React.Component{
}
//getProps.ts
从“typescript”导入*作为ts;
函数isReactComponent(基类型:ts.Type){
if(baseType==null)返回false;
让baseTypeSymbol=baseType.getSymbol();
if(baseTypeSymbol==null)返回false;
//基类型为命名组件
if(baseTypeSymbol.getName()!=“组件”)返回false;
var声明=baseTypeSymbol.declarations;
if(声明==null)返回false;
//位于react模块内部的宣布装置
返回声明.some(r=>r.getSourceFile().fileName.indexOf(“node_modules/@types/react”)!=-1);
}
函数编译(文件名:string[],选项:ts.CompilerOptions):void{
让程序=ts.createProgram(文件名、选项);
让sample=program.getSourceFile(“sample.ts”);
if(sample==null)返回;
let checker=program.getTypeChecker();
//获取文件中的声明
let list=sample.getChildAt(0)作为ts.SyntaxList;
for(设i=0,n=list.getChildCount();i