-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
module: commonJS produces require of .ts file? #8031
Comments
extension should not be necessary, will it work if you use just |
Removed the '.ts' part, and now I get Declaring it as static makes it work again. Which wasn't inmediately apparent, though. Question is now, why does the incorrect behavior -not translating all .ts into .js- persist? That is something I'd like to look into, if there was some kind of guide on looking for this kind of things. Also, why isn't the tooling picking up that I'm calling something statically that exists as a normal method? |
Sample below works without any errors. vladima@vladima-ux31a:~/sources/playground/cjs$ tree
.
├── src
│ ├── app.ts
│ ├── package.json
│ └── tsconfig.json
└── utils
├── sanitizer.ts
└── utils.ts
2 directories, 5 files
vladima@vladima-ux31a:~/sources/playground/cjs$ cat utils/sanitizer.ts
export class Sanitizer{
static sanitize (str) {}
}
vladima@vladima-ux31a:~/sources/playground/cjs$ cat utils/utils.ts
import {Sanitizer} from "./sanitizer";
export default {
Sanitizer: Sanitizer
};
vladima@vladima-ux31a:~/sources/playground/cjs$ cat src/app.ts
import util from "../utils/utils";
util.Sanitizer.sanitize("String");
vladima@vladima-ux31a:~/sources/playground/cjs$ npm install typescript@next --save
npm WARN saveError ENOENT: no such file or directory, open '/home/vladima/sources/playground/cjs/package.json'
/home/vladima/sources/playground/cjs
└── [email protected]
npm WARN enoent ENOENT: no such file or directory, open '/home/vladima/sources/playground/cjs/package.json'
npm WARN cjs No description
npm WARN cjs No repository field.
npm WARN cjs No README data
npm WARN cjs No license field.
vladima@vladima-ux31a:~/sources/playground/cjs$ ./node_modules/.bin/tsc -p src
vladima@vladima-ux31a:~/sources/playground/cjs$
|
@vladimir sorry but no it's not "working". Typescript is Can anyone point me on a good guide to try and make this stop happening?
|
I've made a short sample here |
@cfv1984 I'm curious as to why you wish to specify the extension explicitely and also as to why you expect extension conversion to be part of the compilation process. |
What valid use cases would there be for require'ing ts files in plain js?
|
@aluanhaddad I think its because of the JS / Weback mindset where one does @cfv1984 Please do not use an extension in require for |
I'm genuinely unsure why is it such an awkward thing to want to import a
|
I can understand if you have a desire to do this in Thats why its just about it not being supported https://blogs.msdn.microsoft.com/ericgu/2004/01/12/minus-100-points/ JavaScript modules are still pretty new in the JS lifetime and even ES6 modules come without a loader out of the box (making it effectively a must be transpiled feature). Again its unconventional to add the file extension and I doubt you'd see any big OSS project out there adding the extension. So why add a feature that is unconventional and take on the added complexity of detecting if there is an extension and removing it if there is 🌹 |
@basarat that was what I was getting at. Specifying explicit extensions interferes with/attempts to bypass the TypeScript compiler's module resolution logic and it causes many loaders and build tools to break. I work on projects where some apps are transpiled dynamically in the browser during development and some are transpiled in a node environment. The apps share code without any problems but if we were to specify the extension explicitly everything would break. The insight into the "Webpack Mindset" is interesting. I was asking this question because I was wondering if there was some specific use case where explicitly specifying the extension provided a benefit. As it stands it seems to lower the level of abstraction, and reduce interoperability, portability and maintainability. |
They might be new for some, but they have been around a long time. CommonJS is from 2009, AMD came around 2010 which was largely based on not being able to get CommonJS to understand some of things SystemJS and the the ES6 Module Loader are only discovering now. But AMD was developed because James Burke was trying to improve the ability to load Dojo Modules and Dojo had modules back in 2006, which was based on other modular concepts. 😄
Any use cases are a false economy because of what you rightly point out, it reduces abstraction and portability. It is often the mindset of those who are used to dealing with fixed filesystem resources which are directly addressable (server side mindset) and not recognising the reality where resources are remote and may not have a static physical location. |
@Kitson at this point I can conclude community consensus says the tsc Which is I guess OK, I mean, it appears to be such a cornerstone of the Now, why isn't there an error when I do this? And how would one go and make Also, back in my original post, there is also the issue of my getting an
|
Issue #4595 tracks this request. |
And
Here is a simplification of the code sample: class Sanitizer {
sanitize = function(str){/*various string cleanups*/}
}
Sanitizer.sanitize; // Error : Property `sanitize` does not exist on type `typeof Sanitizer`. I am sure by now you understand why this is an error (you probably wanted it to be a But there is a tradeoff of complexity here (a performance hit of checking if calling |
TypeScript Version:
Version 1.8.9
Code
Expected behavior:
Actual behavior:
Alternatives were tried
Without success. Doing
Yields
Module "project/path/to/Util/Util.ts" resolves to a non-module entity and cannot be imported using this construct.
When running
tsc project/path/to/App.ts
The text was updated successfully, but these errors were encountered: