Unknown type parameter K for typescript internal utilities #2

Closed
opened 2022-11-12 07:22:14 +01:00 by Saul-Mirone · 0 comments
Saul-Mirone commented 2022-11-12 07:22:14 +01:00 (Migrated from github.com)

Give this test file:

export type A = {
    foo: 'bar';
    bar: string;
};
export type B = Omit<A, 'foo'>;

The package will throw an error like:

Error: Unknown type parameter K
    at Context.getType (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:179:23)
    at Context.getTypeParam (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:454:62)
    at Context.getType (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:213:67)
    at Context.itemForSymbol (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:96:22)
    at Context.gatherSymbols (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:28:47)
    at /Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:692:125
    at Array.map (<anonymous>)
    at gatherMany (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:675:18)
    at gather (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:665:12)
    at file:///Users/mirone/Code/unplugin-tsdoc/demo.js:12:15

Additional information:

I found out that the maybeDecl(type.symbol)?.parent?.kind is equal to SyntaxKind.TypeAliasDeclaration here.

    if (type.flags & TypeFlags.TypeParameter) {
      let name = type.symbol.name, found = this.typeParams.find(p => p.name == name)
      if (!found && maybeDecl(type.symbol)?.parent?.kind != SyntaxKind.InferType)
        throw new Error(`Unknown type parameter ${name}`)
      return {type: name, typeParamSource: found ? found.id : this.id}
    }
Give this test file: ```ts export type A = { foo: 'bar'; bar: string; }; export type B = Omit<A, 'foo'>; ``` The package will throw an error like: ```bash Error: Unknown type parameter K at Context.getType (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:179:23) at Context.getTypeParam (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:454:62) at Context.getType (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:213:67) at Context.itemForSymbol (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:96:22) at Context.gatherSymbols (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:28:47) at /Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:692:125 at Array.map (<anonymous>) at gatherMany (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:675:18) at gather (/Users/mirone/Code/unplugin-tsdoc/node_modules/.pnpm/getdocs-ts@0.1.4/node_modules/getdocs-ts/src/index.js:665:12) at file:///Users/mirone/Code/unplugin-tsdoc/demo.js:12:15 ``` Additional information: I found out that the `maybeDecl(type.symbol)?.parent?.kind` is equal to `SyntaxKind.TypeAliasDeclaration` here. ```ts if (type.flags & TypeFlags.TypeParameter) { let name = type.symbol.name, found = this.typeParams.find(p => p.name == name) if (!found && maybeDecl(type.symbol)?.parent?.kind != SyntaxKind.InferType) throw new Error(`Unknown type parameter ${name}`) return {type: name, typeParamSource: found ? found.id : this.id} } ```
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
marijn/getdocs-ts#2
No description provided.