TypeScript: Cannot read property 'length' of undefined in v4 version
The issue happen with v4 version (tested on v4.0.3, v4.0.5 and the latest v4.1.2)
With the same config and code base it compile with no problem in v3.9
Expected behavior:
No such error! No failing at internal level! (Plus it works in v3.9 and not v4)
text should not come as undefined! Or should be handled without problem!
Actual behavior: In v3.9 the code compile with the same config with no problem! In v4 i get Cannot read property ‘length’ of undefined error! And internal failing!
Through the error stack! i get where the error happened and it was at the function computeLineStarts! text is coming undefined! i did some console logging! And i got what file was been treated! And what text node were printing! The file is all normal and correct! A comment at the end was the last thing which after it the code fail!
Then i added another instruction after it! (A variable assignment const someVar = '';) and the error went up! The last treated nodes before failing came more up! Here bellow the file at which my console.logging stopped at!
File on which the resolution failed at:
https://gist.github.com/MohamedLamineAllal/472cf2043a100cb03244de5fb1138034
When i added const someVar = ""; at the end! the last treated node was } from
export interface IWebSocketDriver {
readonly protocol: string;
readonly readyState: number;
readonly url: string;
binaryType: BinaryType;
readonly CONNECTING: number;
readonly OPEN: number;
readonly CLOSING: number;
readonly CLOSED: number;
setGetStreamObj: (getStreamObj: () => WebSocket | IWebSocketDriver) => IWebSocketDriver;
onInit: () => IWebSocketDriver;
onopen: ((this: IWebSocketDriver, ev: Event) => any) | null;
onmessage: ((this: IWebSocketDriver, ev: MessageEvent) => any) | null;
onclose: ((this: IWebSocketDriver, ev: CloseEvent) => any) | null;
onerror: ((this: IWebSocketDriver, ev: Event) => any) | null;
close(code?: number, reason?: string): void;
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
} // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<!!!!!!!!!!!!!!! here the last treated element
/**
* THINGS TO DO:
* =============
*
* decide and define the response output of all the functions
*
* consider a design that add useful information that are exchange platform dependent
*
*
*
* STREAMS :
* Need to precise what form the returned object should have
*
* ok dodo
*/
const someVar = ''; // <=== added this
How i debugged!?
in tsc.js
ts.stringToToken = stringToToken;
function computeLineStarts(text) {
console.log('text: ') // <<<==== here
console.log(text)
var result = new Array();
var pos = 0;
And at
ts.getNonDecoratorTokenPosOfNode = getNonDecoratorTokenPosOfNode;
function getSourceTextOfNodeFromSourceFile(sourceFile, node, includeTrivia) {
console.log('Source file: ') // <<<<<<<<<<<<<<<<<<<<<<<<= here
console.log(sourceFile.path)
console.log('================+++>')
if (includeTrivia === void 0) { includeTrivia = false; }
return getTextOfNodeFromSourceText(sourceFile.text, node, includeTrivia);
}
First Compilation execution (original file)
Without adding the last const someVar = '';
https://gist.github.com/MohamedLamineAllal/653fd4f597aa821f416a33e86d4514eb
Second compilation (after adding the last line)
https://gist.github.com/MohamedLamineAllal/022f6f285313aec30b82bb72238bcaeb
Always it fails at computeLineStarts() method! And because text is undefined.
Error stack
TypeError: Cannot read property 'length' of undefined
at computeLineStarts (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:6528:27)
at Object.getLineStarts (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:6581:60)
at getCurrentLineMap (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:81810:59)
at emitDetachedCommentsAndUpdateCommentsInfo (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:85144:94)
at emitBodyWithDetachedComments (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:85007:17)
at emitSourceFile (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:83873:21)
at pipelineEmitWithHint (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:81887:24)
at noEmitNotification (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:80586:9)
at onEmitNode (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:70565:13)
at onEmitNode (/home/coderhero/.nvm/versions/node/v15.0.1/lib/node_modules/typescript/lib/tsc.js:72333:13)
For the shared links i used typescript Version 4.1.2! Otherwise i tested with v4.0.3 and v4.0.5
tsconfig.json
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./dist",
"module": "commonjs",
"noImplicitAny": true,
"lib": ["ESNext", "DOM"],
"outDir": "./dist",
"target": "es6",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true
},
"typedocOptions": {
"mode": "modules",
"out": "docs"
},
"include": ["src/**/*.ts", "src/**/*.json", "test"],
"exclude": ["node_modules", "dist", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
Related Issues:
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 17
- Comments: 18 (2 by maintainers)
Commits related to this issue
- typescript: rollback to v3.9 This is a rollback pending a release containing a fix for microsoft/TypeScript#41717 We have also encountered another likely unrelated TypeError ('has' property) in emit... — committed to openculinary/frontend by jayaddison 3 years ago
- 移动项目到子文件夹来避免 https://github.com/microsoft/TypeScript/issues/41717 — committed to Icalingua-plus-plus/Icalingua-plus-plus by Icalinguaplusplus 3 years ago
- 移动项目到子文件夹来避免 https://github.com/microsoft/TypeScript/issues/41717 — committed to Icalingua-plus-plus/Icalingua-plus-plus by clansty 3 years ago
i found the redirect condition is check if has same package id, and the incorrect redirect file is with a package.json at same folder, which have the same package name…
maybe the redirct action should only happened in
node_modulesfolder?I’ve had similar issues with 4.1.2, I’ve had switch to 4.0.5 (I was trying to upgrade from 3.9.7)
I have
allowJsset totruesetting it tofalsestopped the error from happening.Pinning
typescriptback tov4.0.5has resolved the issue for me.