-
Notifications
You must be signed in to change notification settings - Fork 520
type inference: ts_library behaves differently than tsc #1013
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
Comments
There are some issues reported in TS repo, so it's hard to say exactly where the problem is
Unfortunately, we had the same problems in our code base. We test some possible fixes like adding paths to modules with there own typings that came not from For now, we use workarounds with importing modules and adding types with |
Encountering the same problem. I'm guessing the issue is ts_library's Rather than using the traditional nodes_modules file layout, it uses path mappings to effectively use a different node_modules. And TypeScript gets confused by how to add imports that reference those files. |
I also created repro here: https://github.com/pauldraper/ts-bazel-infer-import The issue is that when generating |
Additional info: Beginning in TS 3.2, tsc started checking the generated path for node_modules and failing if it found it. Either removing that check, or making the inferred import prefer non-relative paths would fix this issue. |
Note, one possible "escape hatch" is to use plain |
@alexeagle I don't really want to eject from To workaround it, sometimes have to specify function definitions twice. Like: export const handleRequestError: (requestType: string, response: Response, auth?: AuthState) => void = (requestType: string, response: Response, auth?: AuthState) => {
...
} Would definitely be nice to see this fixed so the duplication can be removed. Please let me know if there's any useful info I could provide. |
Just stumbled upon this issue. It would be nice to see some fix. Maybe, a workaround would be to have the ability to set |
Might you be able to go into more details about what that looks like? |
@pope sure! For example, we have import { welcomeUser, blockUser, unblockUser } from '../actions';
const mapDispatchToProps = {
welcomeUser,
unblockUser,
blockUser,
}; Which fails to build with
This problem can be fixed in two ways:
|
Another abosulutely ugly way to prevent the error is by setting the type of the affected object to Is there any progress on this issue? |
For example I get 3 errors from my
and importing those 3 modules into
Is it possible to import those modules once, e.g. in |
@flolu Those errors there you can solve by adding a explicity (rather than implicity) type to whatever is throwing the error.
replace with
This is an actual a |
@Toxicable that is tolerable for your example. But say for example you want to create an NgRx action. It would like like this:
...and I really don't want to annotate hundreds of actions with a multi-line type, which would usually be created automatically 😞 So adding all explicit types would be really painful to maintain.
Is it possible to achieve this with |
The root cause is that ts_library is the google-internal rule for building typescript, and Google doesn't have any node_modules directories at all - all third-party libraries are vendored into the monorepo. |
@alexeagle Ok that's not so good news. Will I am also not so sure why I am only encountering this problem with Angular. For example I use One more question: Is it possible to suppress the errors as they don't seem to take any effect on the running application, maybe we can just ignore the errors? |
Have been running into this issue a lot with styled-components. Mostly with stuff like const css = css`
//
` and const Comp = styled(C)`
//
` we've been solving these with |
I am now using a workaround for this issue by @joeljeske For Typescript 3.8.3 it goes like this:
diff --git a/node_modules/typescript/lib/typescript.js b/node_modules/typescript/lib/typescript.js
index 583f65b..f247520 100644
--- a/node_modules/typescript/lib/typescript.js
+++ b/node_modules/typescript/lib/typescript.js
@@ -90563,6 +90563,7 @@ var ts;
}
ts.isInternalDeclaration = isInternalDeclaration;
var declarationEmitNodeBuilderFlags = 1024 /* MultilineObjectLiterals */ |
+ 67108864 /* AllowNodeModulesRelativePaths */ |
2048 /* WriteClassExpressionAsTypeLiteral */ |
4096 /* UseTypeOfFunction */ |
8 /* UseStructuralFallback */ |
yarn add -D patch-package
{
"scripts": {
"postinstall": "patch-package"
},
} I am not sure if using this patch has any critical downsides? @alexeagle But I can confirm that it "fixes" the issue. |
@flolu glad that worked for you! Would you mind posting your thoughts on the upstream ticket I created (microsoft/TypeScript#37960) so that we can get an official option built in to TS and not require a patch? |
@joeljeske Yeah sure I will post something, that the issue will get more attention. But honestly, I don't really understand what the underlying issue is. So I can't talk about the details. All I know is that it is something I would appreciate being implemented 😂 |
The issue is that Typescript does not think that it is allowed add an import to "react" or "rxjs" or some libs in node_module if it needs to because node_modules is in a different place. It ius only aware of how to add relative paths to the node_module directory. Typically, when you create .d.ts file, they are to be used in a separate repo and therefore cannot rely on relative paths to the node_module directory and the relative path may be incorrect. In my case (as explained in the upstream TS ticket microsoft/TypeScript#37960), I am never shipping .d.ts files for use outside of the repo, they are only used internally in the build as a linking mechanism between each If you were planning to ship your .d.ts files to NPM for example, this patch or this fix should not be used as it will result in invalid .d.ts files for your consumers. The real fix for that scenario is for |
I'm encountering this issue as I try to migrate our large Angular monorepo to use Bazel, using I checked out the angular repository from GitHub and was able to recreate the same problem there by adding a field like @alexeagle you mentioned that Google uses vendored 3rd party dependencies, therefore this particular issue does not arise. What does that look like? How would we do that? Pull in our 3rd pary deps via |
As mentioned in an earlier comment, I am using a tiny patch on typescript directly to disable this behavior. It will only work out safely if you do not work publish the dts files explicitly anywhere. |
@matthewjh here is the patch for the latest Typescript version (3.9.6): diff --git a/node_modules/typescript/lib/typescript.js b/node_modules/typescript/lib/typescript.js
index 73d31bc..b455995 100644
--- a/node_modules/typescript/lib/typescript.js
+++ b/node_modules/typescript/lib/typescript.js
@@ -92621,6 +92621,7 @@ var ts;
}
ts.isInternalDeclaration = isInternalDeclaration;
var declarationEmitNodeBuilderFlags = 1024 /* MultilineObjectLiterals */ |
+ 67108864 /* AllowNodeModulesRelativePaths */ |
2048 /* WriteClassExpressionAsTypeLiteral */ |
4096 /* UseTypeOfFunction */ |
8 /* UseStructuralFallback */ | Just:
|
Reading through all the comments and also some discussions that were going in microsoft/TypeScript#37960 I can definitely see that it is not something that I can do. In my case typescript sources are generated and not hand written and we intend to ship a built npm package that will contain js code and .d.ts types. I still can't wrap my head around the right way to go about this. |
Thanks @flolu and @joeljeske. That worked. Such a shame that we need to do it. I'll put a comment on the Typescript issue -- we really need to make sure it moves forward. |
This issue has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs in two weeks. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs! |
I can confirm this is still an issue and very inconvenient. Hopefully it doesn't auto close. |
This issue has been automatically marked as stale because it has not had any activity for 90 days. It will be closed if no further activity occurs in two weeks. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs! |
This issue was automatically closed because it went two weeks without a reply since it was labeled "Can Close?" |
Still appears to be an issue. Sounds like the current workaround is ts_project or patch the typescript compiler? So, because ts_library is in a sudo deprecated state I assume the preferred path is to move to ts_project. |
Thank you @flolu. For those on Typescript 4.2.3, here's an equivalent patch. diff --git a/node_modules/typescript/lib/typescript.js b/node_modules/typescript/lib/typescript.js
index 84f8b51..5b2ee9a 100644
--- a/node_modules/typescript/lib/typescript.js
+++ b/node_modules/typescript/lib/typescript.js
@@ -98675,6 +98675,7 @@ var ts;
}
ts.isInternalDeclaration = isInternalDeclaration;
var declarationEmitNodeBuilderFlags = 1024 /* MultilineObjectLiterals */ |
+ 67108864 /* AllowNodeModulesRelativePaths */ |
2048 /* WriteClassExpressionAsTypeLiteral */ |
4096 /* UseTypeOfFunction */ |
8 /* UseStructuralFallback */ | |
Uh oh!
There was an error while loading. Please reload this page.
🐞 bug report
Affected Rule
ts_library
Is this a regression?
Uncertain if this is a regression.
Description
When using ts_library on very simple module that imports and make use of a method in ngrx, tsc fails to infer the return type. This contrasts with the vanilla tsc which successfully infers the type and builds the file.🔬 Minimal Reproduction
https://github.com/stephanwlee/bazel-ngc-tsc-bad
Repro steps are detailed in the README.
Note that I am using Bazel 0.26.1.
🔥 Exception or Error
🌍 Your Environment
Operating System:
Output of
bazel version
:Rules version (SHA):
Anything else relevant?
I poked around tsc and tsc_wrapped and inspected which files are being traversed (printed modules that are being resolved by compilerHost). Nothing was out of ordinary for both versions -- i.e., tsc_wrapped also traversed through the typed dependency of @ngrx/store (store/typing.d.ts, store/index.d.ts, ... store/src/model.d.ts)
The text was updated successfully, but these errors were encountered: