ts-morph#TypeChecker TypeScript Examples

The following examples show how to use ts-morph#TypeChecker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From tarojs-router-next with MIT License 6 votes vote down vote up
export function extractType(options: { name: string; declaration: TypeAliasDeclaration; checker: TypeChecker }) {
  const { name, declaration, checker } = options

  if (!TypeAliasDeclaration.is(declaration.getKind())) throw Error(`${name} 应该导出类型定义`)
  let optional = true
  for (const p of declaration.getType().getProperties()) {
    const ds = p.getDeclarations() as PropertySignature[]
    if (ds.length > 1) throw Error('未知错误')
    if (!ds[0].getQuestionTokenNode()) {
      optional = false
      break
    }
  }

  const text = checker.getTypeText(declaration.getType(), declaration.getTypeNode(), ts.TypeFormatFlags.InTypeAlias)

  return {
    text,
    optional,
    name,
  }
}
Example #2
Source File: index.ts    From tarojs-router-next with MIT License 6 votes vote down vote up
export function extractValue(options: { name: string; declaration: VariableDeclaration; checker: TypeChecker }) {
  const { name, declaration, checker } = options

  if (!VariableDeclaration.is(declaration.getKind())) throw Error(`${name} 应该导出变量类型`)

  const text = declaration.getFullText()

  return text.split('=', 2)[1].trim()
}