Consider the following interface:
interface X {
x: string
}
I'm trying to use the typescript compiler API to get property x
's type. Here's what I have so far:
import {
PropertySignature,
createSourceFile,
ScriptTarget,
ScriptKind,
SyntaxKind,
InterfaceDeclaration,
Identifier,
} from 'typescript'
describe('Compiler test', () => {
it('should be able to find type information about X.x', () => {
const sourceText = 'interface X { x: string }'
const ast = createSourceFile('source.ts', sourceText, ScriptTarget.ES5, false, ScriptKind.TS)
const interfaceX = ast
.getChildAt(0)
.getChildren()
.find((child) => child.kind === SyntaxKind.InterfaceDeclaration) as InterfaceDeclaration
const propX = interfaceX.members.find((member) => (member.name as Identifier).escapedText === 'x')
console.log(JSON.stringify(propX, null, 2))
})
})
Now the content of the propX
node is the following:
{
"pos": 13,
"end": 23,
"flags": 0,
"kind": 151,
"name": {
"pos": 13,
"end": 15,
"flags": 0,
"escapedText": "x"
},
"type": {
"pos": 16,
"end": 23,
"flags": 0,
"kind": 137
}
}
From which the name of the node is clearly extractable, however the type node doesn't seem to have any information that's useful.
How would I get the type information of the property? All I need is "string"
.
So the way to do it was to build a
Program
(need aCompilerHost
to do it) and use theTypeChecker
as @MattMcCutchen suggested:The
CompilerHost
(you don't need a class implementation, but I found it more convenient):Build a
Program
:Find the interface and property like in the question (only difference is the way of accessing the SourceFile):
Lastly get the type:
Where
propX
is the same variable as in my question.