ts-json-schema-generator
ts-json-schema-generator copied to clipboard
"typeSymbol.declarations is undefined"
node_modules/ts-json-schema-generator/dist/src/NodeParser/TypeReferenceNodeParser.js:52 return this.childNodeParser.createType(typeSymbol.declarations.filter((n) => !invalidTypes[n.kind])[0], this.createSubContext(node, context));
types:
` /**
* 自定义组件路由对象
* @description 可获得当前自定义组件的路由对象,路由方法与全局路由方法功能相同,唯一区别在于调用时,相对路径是相对于该自定义组件
* @version 2.7.22
*/
readonly router: Shared.IRouter;
`
Thanks for the report. Can you provide a minimal reproducible example that demonstrates the issue?
I was trying to figure out how to fix this problem, I had a type:
import { Namespace } from "@my-company/pnpm-monorepo-pkg"
export APIType = Namespace.Type;
Namespace.Type; <- node type "ref" left="Namespace" right="Type"
typeSymbol = this.typeChecker.getSymbolAtLocation(node.typeName) // TypeAlias
// ok, let's resolve it
// same as typeSymbol.links
aliasedSymbol = this.typeChecker.getDeclaredTypeOfSymbol(typeSymbol);
// ok, aliasedSymbol type flag is "unresolved"
typeSymbol.links.flag is "unresolved", so it means that import { Namespace } from "@my-company/pnpm-monorepo-pkg" is not resolved.
But my ts compiler says that it's ok, because my fs looks like:
dist/index.d.ts
and pkg.json is:
"name": "@my-company/pnpm-monorepo-pkg",
"version": "1.0.0",
"scripts": {
"build": "tsup src/index.ts --format esm --dts",
},
"sideEffects": false,
"type": "module",
"main": "./dist/index.js",
"types": "./src/index.ts",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./*": {
"import": [
"./dist/*.js",
"./dist/*/index.js"
],
"require": [
"./dist/*.cjs",
"./dist/*/index.cjs"
]
}
},
"typesVersions": {
"*": {
"*": [
"src/*",
"src/*/index"
]
}
},
"publishConfig": {
"typesVersions": {
"*": {
"*": [
"dist/*.d.ts",
"dist/*/index.d.ts"
]
}
}
},
"files": [
"dist"
],
and imported like:
"dependencies": {
"@my-company/pnpm-monorepo-pkg": "workspace:*",
}
However, the generation works with packages downloaded from npm.
I tried changing the export from dist/index.d.ts to src/index.ts. That didn't work either.
And without any wrappers:
// TypeA = { a: 3 }
import { TypeA } from "@my-company/pnpm-monorepo-pkg"
export APIType = TypeA; // generated output is APIType: {}
So, "typeSymbol.declarations is undefined" means that "declaration for type is not defined".
The problem is probably related to symbolic links pkg ref.
The easiest way to find out about this problem was to put skipTypeCheck: false.
And probably the problem is the wrong path to ts-config. Surprisingly, if you do not specify the path to ts-config, everything works fine, except for importing local packages into a monorepo.
So, I'm 99% sure that this is the fix: tsconfig: path.resolve(import.meta.dirname, "path/to/tsconfig.json"),.
And this is the repo from original issue:
https://github.com/ant-mini-program/api-typings/blob/ffe0d436b5e7d9b12cdd4a1cb4081a3e235b3a73/packages/global/types/lib.component.d.ts#L180-L185
https://github.com/ant-mini-program/api-typings/blob/ffe0d436b5e7d9b12cdd4a1cb4081a3e235b3a73/packages/global/types/index.d.ts#L5
/// <reference path="./lib.shared.d.ts" /> <--- probably not readable namespace import for Shared.IRouter;