question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RTK generateEndpoints throws typescript error: Trace: TypeError: Cannot read property 'pos' of undefined

See original GitHub issue

Running the RTK code generation as described in the docs by running npx @rtk-query/codegen-openapi openapi-config.ts works fine without any issues.

However, running exactly the same config using the generateEndpoints function produces what looks like a typescript error. I’ve tried running it both using Node and esbuild-runner but it still errors. I’ve tried using different openapi endpoints but I still receive the same error when trying to run the function programmatically.

Setup

// src/store/emptyApi.ts
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

export const emptySplitApi = createApi({
  baseQuery: fetchBaseQuery({ baseUrl: '/' }),
  endpoints: () => ({}),
});
// apiConfig.ts
import type { ConfigFile } from '@rtk-query/codegen-openapi';

const apiConfig: ConfigFile = {
  // Using an example openapi endpoint found online
  schemaFile: 'https://petstore.swagger.io/v2/swagger.json',
  apiFile: './src/store/emptyApi.ts',
  apiImport: 'emptySplitApi',
  outputFile: './src/model/generated/api.ts',
  exportName: 'api',
  hooks: true,
};

export default apiConfig;
// generateApi.ts (run through node or esbuild-runner)
const config = {
  schemaFile: 'https://petstore.swagger.io/v2/swagger.json',
  apiFile: './src/store/emptyApi.ts',
  apiImport: 'emptySplitApi',
  outputFile: './src/model/generated/api.ts',
  exportName: 'api',
  hooks: true,
};

generateEndpoints(config).catch(console.trace);

Output

Trace: TypeError: Cannot read property 'pos' of undefined
    at emitNodeList (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:111266:48)
    at emitList (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:111158:13)
    at emitObjectLiteralExpression (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:109538:13)
    at pipelineEmitWithHintWorker (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:108863:32)
    at pipelineEmitWithHint (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:108479:17)
    at pipelineEmitWithComments (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:112033:13)
    at pipelineEmit (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:108419:13)
    at emitExpression (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:108403:13)
    at emitNodeList (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:111271:25)
    at emitExpressionList (/Users/***/***/node_modules/@rtk-query/codegen-openapi/node_modules/typescript/lib/typescript.js:111161:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
kdevancommented, Jun 24, 2022

The function that throws the error seems mostly the same between these versions:

https://raw.githubusercontent.com/microsoft/TypeScript/v4.5.2/lib/typescript.js

function emitTupleType(node) {
    emitTokenWithComment(22 /* OpenBracketToken */, node.pos, writePunctuation, node);
    var flags = ts.getEmitFlags(node) & 1 /* SingleLine */ ? 528 /* SingleLineTupleTypeElements */ : 657 /* MultiLineTupleTypeElements */;
    emitList(node, node.elements, flags | 524288 /* NoSpaceIfEmpty */);
    emitTokenWithComment(23 /* CloseBracketToken */, node.elements.end, writePunctuation, node);
}

https://raw.githubusercontent.com/microsoft/TypeScript/v4.7.4/lib/typescript.js

function emitTupleType(node) {
    emitTokenWithComment(22 /* SyntaxKind.OpenBracketToken */, node.pos, writePunctuation, node);
    var flags = ts.getEmitFlags(node) & 1 /* EmitFlags.SingleLine */ ? 528 /* ListFormat.SingleLineTupleTypeElements */ : 657 /* ListFormat.MultiLineTupleTypeElements */;
    emitList(node, node.elements, flags | 524288 /* ListFormat.NoSpaceIfEmpty */, parenthesizer.parenthesizeElementTypeOfTupleType);
    emitTokenWithComment(23 /* SyntaxKind.CloseBracketToken */, node.elements.end, writePunctuation, node);
}

Can see that node.elements.end is the offending object/array. I’ll keep digging and update if I find anything.

Edit: Looks to me like filterEndpoints and endpointOverrides are the two possible properties that can be undefined and also use a possible List/NodeList and Tuple type? The stack trace mentions a UnionType -> List -> NodeList -> Tuple. I need to fix some stuff in my project and then I can test this theory out, I’ll update as soon as I can.

0reactions
kdevancommented, Jul 3, 2022

Yeah, in my project oazapfts itself is using typescript 4.7.4 while rtk-query-codegen-openapi is using 4.5.5. Since oazapfts is used for typescript generation, nodes are being generated using 4.7.4 and then being processed by this library using 4.5.5.

In my case this is due to how I have yarn set up, using yarn 3.2.1 workspaces with nodeLinker: node-modules in .yarnrc.yml.

I tried adding a resolution to force everything to use 4.7.4 and everything is working so far:

"resolutions": {
  "typescript": "4.7.4"
}

Obviously this is risky to force libraries to use a version they don’t expect but for now it’s allowing development on this front to continue. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

"TypeError: Cannot read property 'pos' of undefined" after ...
I'm trying to clear data from the all charts (Highcharts) in my Angular application. After all i am getting an error as in...
Read more >
How to Fix TypeError: Cannot read Property 'push' of ...
You call the method on a variable previously set to undefined . You call the method on a variable before it has been...
Read more >
How to Avoid the Infamous "Cannot read properties of ... - Bitovi
As a JavaScript developer, I'm sure you've encountered the frustrating runtime TypeError Cannot read properties of undefined . TypeScript ...
Read more >
How to Read React Errors (fix 'Cannot read property of ...
Got an error like this in your React component? Cannot read property `map` of undefined. In this post we'll talk about how to...
Read more >
Avoiding those dang cannot read property of undefined errors
Uncaught TypeError: Cannot read property 'foo' of undefined.​ The dreaded error we all hit at some point in JavaScript development.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found