Skip to content

关于typescript类型检查失败Cannot read property 'length' of undefined #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
SepVeneto opened this issue Oct 15, 2021 · 1 comment

Comments

@SepVeneto
Copy link
Owner

SepVeneto commented Oct 15, 2021

项目改为monorepo结构后,在进行类型检查的时候typescript抛出

TypeError: Cannot read properties of undefined (reading 'length')
TypeError: Cannot read properties of undefined (reading 'length')
    at computeLineStarts (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:9441:27)
    at Object.getLineStarts (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:9501:60)
    at getCurrentLineMap (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:100407:59)
    at emitDetachedCommentsAndUpdateCommentsInfo (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:104074:94)
    at emitBodyWithDetachedComments (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:103923:17)
    at emitSourceFile (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:102638:21)
    at pipelineEmitWithHint (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:100488:24)
    at pipelineEmit (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:100440:13)
    at print (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:100374:13)
    at Object.writeFile (node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:100358:13)

解决方案:
避免不同工作空间引用同一个工作空间

以这个项目为例
本身是一个后台管理系统,通过subtree引入了两个包,分别是dynamic-form和basic-components

\website
\packages
    \dynamic-form
    \basic-components
        \@basic-components/components

dynamic-form和basic-components本身也是monorepo,有自己的工作空间

结构本身没什么问题,理论上website用到的组件都是通过basic-components导出后的,但是有的地方会用到basic-components的组件的类型声明,比如@basic-components/components/search/type,而basic-components本身并没有导出组件对应的类型声明,因此为了满足类型检查的条件,需要在website中引入@basic-components/components

\website
+     \@basic-components/components
\packages
    \dynamic-form
    \basic-components
        \@basic-components/components

这样就会遇到上面类型检查找不到文件的问题
对于这种不同工作空间引入的包名相同的,可以直接外部根目录引入

\website
-     \@basic-components/components
\packages
    \dynamic-form
    \basic-components
-        \@basic-components/components
+ \@basic-components/components

===================================================================================
定位了一下抛出问题的地方

function computeLineStarts(text) {
    var result = new Array();
    var pos = 0;
    var lineStart = 0;
    /* ------------------------这里text的值为undefined----------------------------- */
    while (pos < text.length) {
        var ch = text.charCodeAt(pos);
        pos++;
        switch (ch) {
            case 13 /* carriageReturn */:
                if (text.charCodeAt(pos) === 10 /* lineFeed */) {
                    pos++;
                }
            // falls through
            case 10 /* lineFeed */:
                result.push(lineStart);
                lineStart = pos;
                break;
            default:
                if (ch > 127 /* maxAsciiCharacter */ && isLineBreak(ch)) {
                    result.push(lineStart);
                    lineStart = pos;
                }
                break;
        }
    }
    result.push(lineStart);
    return result;
}

翻了一下github,也有不少人遇到了类似的问题,大概看了一下,出现问题的原因好像是因为不同工作空间引入了同一个或名称相同的依赖包,简单的说就是package.json的name相同。
导致做类型检查读取文件的时候会重定向到同一个目录里,但是实际上由于工作空间的存在,实际路径是对不上的,导致读取不到文件内容。
https://github.com/microsoft/TypeScript/issues/41717#issuecomment-781128223

但是如果引用的是第三方依赖,比如从npm下载的,不知道会不会出现这个问题。话又说回来,正常的第三方依赖一般都是编译后甚至是压缩过的,不太会出现。因为这个问题本质是typescript找不到需要进行类型检查的问题,而从一开始就引用js文件自然就不需要进行类型查检了。

这么一想的话,难怪issues里有人提出来tsconfig.jsallowJs关闭后就解决了这个问题,可能也是这个原因,没有从根本上解决,只是跳过这检查。

最后吐槽一下官方,这个bug的修复版本从4.2.1改再改,4.3.1的时候被标注允许修复,结果又跳票到4.5。

@GTRgoSky
Copy link

GTRgoSky commented May 8, 2022

偶然发现你这个回复,话说我在4.6.4版本中遇到了这个问题,4.3.5是没问题的。我们并没有不同工作空间引用同一个工作空间。
我们是一个electron项目,由render和main两个打包入口,不知道这是不是引起问题的原因,如果想定位,有什么好办法可以去定位问题呢~
很确定的是,我们的错误都是一个,computeLineStarts的text是undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants