You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Go to Definition for Non JS/TS File Imports #15146
They're a couple of great VS Code plugins that do this at the moment.
I assume they work by returning a "definition" that maps to a file and that VS Code then seems adds this as a possible definition to navigate to (I don't really know if this is how it works, but this is how it appears).
Because of this, they suffer from the following problem:
Say I have a declaration file, let's call it ambiance.d.ts with the following content
in this case, despite of the plugins efforts, the user always sees a list of two definitions to go to instead of being able to jump directly to the file.
That is really the only drawback to the current plugins.
Having said that, I always thought it would be nice if TypeScript could validate the existence of these asset files but i think that ship has long ago sailed.
My main concern is that this is not ES6 compliant. ES6 does not talk about importing non-code modules. So this code is not really valid ES6 and will not work with a native ES6 module implementation.
So we can pretend that it will work, and just let it pass. or leave it the way it is and be spec-compliant.
I'm OK with not including this as builtin TS functionality given the compliance concerns. Is there any way we can leverage TSServer for these scenarios however? Perhaps with a TSServer plugin?
Since this is specific to Webpack's implementation of require(), I would argue that it should go in @types/webpack-env, not core TypeScript. Isn't that how this is supposed to work?
I should have said, since it's specific to the loader / bundler's implementation of require(). I was under the impression that TS didn't provide a native capability to require text resources, but maybe I misunderstood.
I should have said, since it's specific to the loader / bundler's implementation of require(). I was under the impression that TS didn't provide a native capability to require text resources, but maybe I misunderstood.
It is correct that TypeScript does not provide this capability, but Webpack is just one of many tools that do.
I think my point stands, in that case -- nothing in core TypeScript provides for require("foo.png") (or css or html or whatever) so it shouldn't be in an ambient declaration unless I've specifically added typings for an environment that does provide that ability. Right?
Additionally TypeScript should be checking the path of EVERY import that occurs (module or otherwise) and raise an appropriate error. A hacky workaround silences an error that an html file isn't a module, but an incorrect relative path doesn't re-flag an error. So I'm stuck guessing that my import paths are hopefully correct.
I don't get the argument regarding strict ES6 spec compliance. This is something webpack, fusebox and pretty much every bundler out there allows and encourages. I'd imagine this has plenty of devs out there using it. Widespread real-world use cases matter, so the meaning of what is and isn't 'compliant' needs to be re-evaluated in this case.
Besides if this needs to be supported by the core, and maybe just have an appropriate .tsconfig flag turned off by default, why not just do it?
Reacted by Seivan, Mikhail Nekrasov, Édouard Lopez, Josh Manning, 流浪大法师 and Dan Orzechowski
I had read this issue earlier today, before I filed issue 90155 in vscode . This does not appear to me to address my problem.
But if, as you said earlier in this thread, there are extensions that workaround this I'd sure like to know of one. I've been scouring the extensions gallery trying to find it.
It is very important to autocomplete files other than JS or TS and check the existence of a file. Due to its absence, typos often appear and, in general, work becomes less comfortable. Please make this opportunity.
Yes, go to definition works. But how can I get rid of the warning "Cannot find module '../img/bla.png' or its corresponding type declarations.ts(2307)" while preserving this feature?
If I declare module '*.png' in global.d.ts the warning disappears, but then I'm again unable to use go-to-definition.
Reacted by James Bromwell, cuiyajie, Aziz Gazanchiian, aiello, Rim (Y Nguyen), Cleves and musjj
Unfortunately, adding { export default '' as any; } doesn't help. When I use go-to-definition, I end up in global.d.ts (which might be right, as there is the definition), but I want to open the file I'm importing. 🤷♂️
With universal(ish) support for real ESM, do a lot of folks still use these fake resource import / require statements that only work in bundlers? Seems to me that if the bundlers want to let you inject a resource inline, they should come up with a new keyword instead of overloading one that your runtime understands. (Or, y'know, just get the resource with fetch or fs.read*)
Reacted by fregante, Lexi Robinson and Ivan Nikolić
Activity
aluanhaddad commentedon Apr 12, 2017
They're a couple of great VS Code plugins that do this at the moment.
I assume they work by returning a "definition" that maps to a file and that VS Code then seems adds this as a possible definition to navigate to (I don't really know if this is how it works, but this is how it appears).
Because of this, they suffer from the following problem:
Say I have a declaration file, let's call it
ambiance.d.tswith the following contentin this case, despite of the plugins efforts, the user always sees a list of two definitions to go to instead of being able to jump directly to the file.
That is really the only drawback to the current plugins.
Having said that, I always thought it would be nice if TypeScript could validate the existence of these asset files but i think that ship has long ago sailed.
mhegazy commentedon Apr 19, 2017
My main concern is that this is not ES6 compliant. ES6 does not talk about importing non-code modules. So this code is not really valid ES6 and will not work with a native ES6 module implementation.
So we can pretend that it will work, and just let it pass. or leave it the way it is and be spec-compliant.
mjbvz commentedon Apr 19, 2017
I'm OK with not including this as builtin TS functionality given the compliance concerns. Is there any way we can leverage TSServer for these scenarios however? Perhaps with a TSServer plugin?
mhegazy commentedon Apr 19, 2017
This is part of the module resolution logic, so we have to add it in the core.
thw0rted commentedon Aug 3, 2017
Since this is specific to Webpack's implementation of
require(), I would argue that it should go in@types/webpack-env, not core TypeScript. Isn't that how this is supposed to work?aluanhaddad commentedon Aug 6, 2017
James Bromwell (@thw0rted) how is this specific to Webpack?
thw0rted commentedon Aug 6, 2017
I should have said, since it's specific to the loader / bundler's implementation of
require(). I was under the impression that TS didn't provide a native capability to require text resources, but maybe I misunderstood.aluanhaddad commentedon Aug 7, 2017
James Bromwell (@thw0rted)
It is correct that TypeScript does not provide this capability, but Webpack is just one of many tools that do.
thw0rted commentedon Aug 7, 2017
I think my point stands, in that case -- nothing in core TypeScript provides for
require("foo.png")(or css or html or whatever) so it shouldn't be in an ambient declaration unless I've specifically added typings for an environment that does provide that ability. Right?aluanhaddad commentedon Aug 7, 2017
Yes you are quite correct. I didn't mean to suggest otherwise I was just mentioning that the notion is more general than Webpack.
jpike88 commentedon Apr 19, 2018
Additionally TypeScript should be checking the path of EVERY import that occurs (module or otherwise) and raise an appropriate error. A hacky workaround silences an error that an html file isn't a module, but an incorrect relative path doesn't re-flag an error. So I'm stuck guessing that my import paths are hopefully correct.
microsoft/vscode#48025 (comment)
I don't get the argument regarding strict ES6 spec compliance. This is something webpack, fusebox and pretty much every bundler out there allows and encourages. I'd imagine this has plenty of devs out there using it. Widespread real-world use cases matter, so the meaning of what is and isn't 'compliant' needs to be re-evaluated in this case.
Besides if this needs to be supported by the core, and maybe just have an appropriate .tsconfig flag turned off by default, why not just do it?
15 remaining items
joshglenn commentedon Feb 7, 2020
I had read this issue earlier today, before I filed issue 90155 in vscode . This does not appear to me to address my problem.
But if, as you said earlier in this thread, there are extensions that workaround this I'd sure like to know of one. I've been scouring the extensions gallery trying to find it.
Do you know any by name that I could try?
DMQ commentedon Mar 13, 2020
I found a vscode plugin called
file peekto provide this functionality. Hope also can help you guys.Cpanmac commentedon Apr 17, 2020
When can we deal with this problem?Matt Bierner (@mjbvz)
mishannn commentedon Aug 19, 2020
It is very important to autocomplete files other than JS or TS and check the existence of a file. Due to its absence, typos often appear and, in general, work becomes less comfortable. Please make this opportunity.
FrederickEngelhardt commentedon Nov 2, 2020
Bump. It would be nice to have a way to do file path resolution with wild cards and extrapolate the beginning path and resolve the file using node.
The above would both declare the typing and return the file it should be resolving. Of course there would need to be escape hatches for bad paths etc.
edouard-lopez commentedon Mar 12, 2021
In the meantine, use can leverage CSS Navigation extension className resolution.
It planned to support
importresolution in a future release.dummdidumm commentedon Jul 10, 2021
I think this can be closed since this landed in TS 4.3 https://devblogs.microsoft.com/typescript/announcing-typescript-4-3/#go-to-def-non-js
Yegorich555 commentedon Jul 12, 2021
Yep. I've just rechecked. Looks lite it works now
chkpnt commentedon Sep 16, 2021
Yes, go to definition works. But how can I get rid of the warning "Cannot find module '../img/bla.png' or its corresponding type declarations.ts(2307)" while preserving this feature?
If I
declare module '*.png'inglobal.d.tsthe warning disappears, but then I'm again unable to use go-to-definition.NerdPraise commentedon Feb 1, 2022
Gregor Dschung (@chkpnt) A workaround; not too good but it works
declare module '*.png' { export default '' as any; }Removes the warning and also go-to-definition
chkpnt commentedon Feb 8, 2022
Unfortunately, adding
{ export default '' as any; }doesn't help. When I use go-to-definition, I end up inglobal.d.ts(which might be right, as there is the definition), but I want to open the file I'm importing. 🤷♂️aleclarson commentedon Jun 6, 2024
Here's an extension that implements the requested feature:
https://marketplace.visualstudio.com/items?itemName=thisismanta.third-eye
shankarThiyagaraajan commentedon Jun 23, 2024
It’s not solving the '.scss' navigation to file and style class.
Issue still exist.
fregante commentedon Oct 4, 2025
This seems to work for relative paths (
import "./file.scss") but not for node_modules imports:thw0rted commentedon Oct 4, 2025
With universal(ish) support for real ESM, do a lot of folks still use these fake resource import / require statements that only work in bundlers? Seems to me that if the bundlers want to let you inject a resource inline, they should come up with a new keyword instead of overloading one that your runtime understands. (Or, y'know, just get the resource with fetch or fs.read*)