-
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
Support question - declaring RequireJS deps / telling TSC about RequireJS deps #13606
Comments
Use
Set |
thanks so much, let me try this |
thanks, I looked at the module-resolution #path-mapping section so I tried this (you can see the paths are CDN urls, they are not paths to node_modules, although I suppose I could add those if necessary to get things to compile) {
"compilerOptions": {
"baseUrl": "public",
"paths": {
"rxjs": [
"//cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.1/Rx"
],
"async": [
"//cdnjs.cloudflare.com/ajax/libs/async/2.1.4/async"
],
"react-dom": [
"//cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom"
],
"react": [
"//cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-with-addons"
],
"socketio": [
"//cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.2/socket.io"
],
"@hot-reload-handler": [
"js/hot-reload-handler"
],
"@hot-reloader": [
"js/hot-reloader"
],
"tslib": [
"vendor/tslib"
],
"immutable": [
"//cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable"
],
"react-redux": [
"//cdnjs.cloudflare.com/ajax/libs/react-redux/4.4.5/react-redux"
],
"redux": [
"//cdnjs.cloudflare.com/ajax/libs/redux/3.5.2/redux"
]
},
"types": [
"node",
"lodash",
"react",
"react-dom",
"redux",
"react-redux",
"async",
"requirejs" // <<< added this, and npm installed @types/requirejs
],
"typeRoots": [
"node_modules/@types"
],
"compileOnSave": true,
"target": "es5",
"module": "amd",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"allowJs": true,
"allowUnreachableCode": true,
"moduleResolution": "classic",
"lib": [
"es2015",
"dom"
]
},
"include": [
"./public/**/*.ts",
"./public/**/*.tsx"
]
} If I use the above tsconfig.json I still get: and any idea what might be up? thanks for your support if the pictures are too hard to read please let me know I assume that something else should go in the paths arrays, besides the URLs pointing to CDN? The link you gave me didn't see to make it clear what to put in there. |
I think this might answer my question: it says:
might that solve my problem? Do I really have to put those declarations in all my files which reference <script> tag imports? |
Unfortunately those |
your paths section is not correct. the compiler will not look up your CDN files. I think I was not clear. your requirejs config stays the way it is, the compiler is not going to write that for you. the compiler will try to find your modules in A few things from your config file:
|
here is what i think you need:
here is what i think your config file should look like: {
"compilerOptions": {
"baseUrl": "public",
"paths": {
"@hot-reload-handler": [ "<path to .d.ts for js/hot-reload-handler>"],
"@hot-reloader": ["<path to .d.ts for js/hot-reloader>"]
},
"types": [
"node",
"lodash",
"react",
"react-dom",
"redux",
"react-redux",
"async",
"requirejs",
"immutable"
],
"target": "es5",
"module": "amd",
"removeComments": true,
"allowUnreachableCode": true,
"moduleResolution": "node",
"lib": [
"es2015",
"dom"
]
},
"include": [
"./public/**/*.ts",
"./public/**/*.tsx"
]
} let me know if this helped. |
now i see what import * as Application as from "js/application"
Application.start(); the compiler will generate a hot-reloader.js file that has the |
The TS docs say that for AMD the module resolution should be 'classic'; but I guess since I am using ES6 import syntax (for the most part), I should use moduleResolution: 'node'? |
I did not know where the
sorry for the confusion. |
so is it working now? |
my code runs fine; but it still compiles with unnecessary errors my goal is to get 0 compile errors (obviously) but I still see a few related to RequireJS/TS integration:
TS doesn't recognize these modules even though they are OK. I am still looking for a mechanism to tell the tsc compiler about the modules that are loaded via the requirejs.config paths. There must be a heavy-handed way to just tell tsc that these imports are loaded via RequireJS network dependencies.
Also I see this error unrelated to RequireJS:
I am trying to get rid of that error, how can I turn off strict mode with |
All of the above errors should be avoidable. I would really love some help to fix these.. Can I share my project with you? |
you do not need this.
did you add a path mapping entry for it? e.g. "paths": {
"@hot-reload-handler": [ "js/hot-reload-handler"]
}, or just "paths": {
"@*": [ "js/*"]
},
I think the error there is self-explanatory, you either need to make this a |
nice, thanks let me try it, your explanation is clearer now but this line of code:
is not in my project, I think it's a dependency...I think it's in here |
It's really close to working is there a way to do something like this: "paths": {
"@config": [ false]
}, or "paths": {
"@config": [ "empty:"]
}, I just want to ignore module resolution for the |
I would really love it if TS had 1st class support for RequireJS / AMD :) |
Why are you using the node.js types if you're using RequireJS? That's the source of the |
@blakeembrey you're probably right! for my front-end tsconfig I should probably get rid of
|
I have two tsconfig.json files, one for backend one for frontend; the frontend config only includes the public directory, the backend config is everything but public directory. I tried putting requirejs for the frontend config and node for the backend config and I get the same issues |
in a .d.ts file add: declare module "@config"; |
what is your |
yep, will share, thanks; note that I have https://github.com/ORESoftware/hr4r2 to build the project you can use
and you should see the compile errors I mention above |
since Also regarding:
I am not sure if that will work for RequireJS since that will create a module, but what I need is an executable file/script. So I left that code alone without altering it to TS module syntax code. |
So the main issues were in tsconfig. PR sent to address these: ORESoftware/hr4r2#1 the error message you were getting about require with "subsequent declarations" is because the remaining issues seem self-explanatory to me. |
Fixes for issues reported in microsoft/TypeScript#13606
I think you can define a buffer in both, just make sure they are structurally compatable. |
@mhegazy thanks so much for helping me out with this, the project compiles much better now one thing I don't understand you pared this (in tsconfig-fe.json): "types": [
"lodash",
"react",
"react-dom",
"redux",
"react-redux",
"redux-thunk",
"async",
"requirejs",
"socket.io",
"socket.io-client",
"immutable",
"uuid",
"whatwg-fetch"
], down to this: "types": [
"react",
"react-dom",
"whatwg-fetch",
"requirejs"
], but I am using thanks! |
It's not a buffer on the front-end though, it's an array. It's only a buffer in node and TypeScript doesn't support the ability to define universal module like that. |
Then there should be |
|
@mhegazy How would that work when TypeScript doesn't know which one to resolve when you install Edit: Sorry for the thread within the thread, I'm happy to move this to the other issue I have open. I just don't see how you intend to workaround this when people need to able to use TypeScript with universal modules. |
it won't :), you will have to add a mapping from |
@mhegazy In the path mapping section? If that works, it sounds like a decent solution. I guess the same goes for a module author then, tell their TypeScript users to use path mapping if they're using a browser? E.g. Edit: If it helps, I do have half a dozen examples of this that I'm stuck on supporting with TypeScript. For instance, I want to provide support for browsers using https://github.com/blakeembrey/node-servie as request/response interfaces but I can't today because TypeScript doesn't know how to require the browser version (doesn't expose the buffer method but might support blobs, for instance). Even if path mapping worked here, it'd just break when a different module is published using |
@blakeembrey I'd like to subscribe to that other issue. Can I have a link 🌹 |
I am having trouble finding good information about using TypeScript with RequireJS (I am a big fanboy of AMD/RequireJS) - I have this question on SO:
http://stackoverflow.com/questions/41761172/declaring-requirejs-dependencies-with-typescript
installing @types/redux helps but it doesn't solve the problem entirely. My primary concern is twofold:
(1) how do we tell TypeScript about the globals "require"/"requirejs" that exist in the main requirejs load script? As you can see in the images, Webstorm highlights them as red because they are not recognized by TS. (If you look at the image below, I am not sure why requirejs is red, the first
require
is recognized, but the secondrequire
gets a typing error saying "invalid number of arguments, 1 expected".)(2) how do we tell TypeScript about the dependencies that are loaded as paths in the requirejs.config({paths:{}}) object? For example, how do I tell TS that 'immutable' or 'rxjs' is loaded as a network dependency and is not loaded until runtime?
e.g.:
please advise, thanks very much! once I figure this out, I will publish an article on it, to share the knowledge
The text was updated successfully, but these errors were encountered: