I import the style file in my project like this:
import styles from '#src/styles.scss';
The tsconfig.json file contains:
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES5",
"jsx": "react",
"baseUrl": "./",
"paths": {
"#src/*": ["src/*"]
}
},
"include": ["src"],
"exclude": ["node_modules"],
"compileOnSave": false
}
But my Visual Studio Code is throwing error Cannot find module "#src/styles.scss". Although the assembly of the project occurs without errors. The styles.scss file does exist and is located in the src folder.
In the project I use webpack 5 and I have already specified aliases in its config.
Can you please tell me how to solve this visual problem?
Following our chat, I think that the reason IntelliSense is not working is that typescript paths look for .js(x)/.ts(x) extensions (which is not the case of .scss). The reason it works at build time is that webpack is taking care of it.
I recommend using a solution like module-alias to get rid of this warning on VSCode.
After a few days of searching for a solution, I still found it! The whole problem is that typescript only accepts files with .js(x)/.ts(x) extensions as modules.
Therefore, we need to manually specify that files with the .scss extension are also modules.
Create a typings folder in the root of the project. In this folder, create a declarations.d.ts file with the contents:
declare module '*.scss' {
const content: Record<string, string>;
export default content;
}
Now in the tsconfig.json file, add the typings folder:
{
...
"include": ["typings", "src"],
}
I hope my answer saves someone time!
Related
I'm using Cypress in TypeScript, in WebStorm. I'd been working on project A1 for a few weeks, I added Cypress, I add TS to Cypress, and then things started getting a little funny, including js files being added alongside the ts files, both in Cypress and in my src folder. (tsx files didn't do this, only ts)
So I restarted the project: I created a whole new Create React App project, A2, and started dragging the files one by one (or folder by folder) from A1 to A2. I added the Cypress files to A2 by running cypress open when A2 had no cypress files in it, which caused Cypress to add its files. Then I added my tests from A1 (copying code into new files, not copying the files) and set up various config files and stuff such that:
TS recognizes my custom cypress commands
No extra JS files are created
Then I deleted all the files in the A1 project and dragged in all the files, minus the node_modules folder, from A2, and ran yarn install.
Everything works, except the Cypress folder still creates js files alongside the ts files.
It's very hard to pinpoint the actual moment when this happens: I had written this post and abandoned it because things seemed to be working fine. But then a few hours later I noticed the js files were back! Only in the Cypress folders, including ALL ts files in the Cypress folders (/integration, /plugins, etc).
Here's some tsconfigs:
cypress/tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress"]
},
"include": [
"**/*.ts", "**/*.d.ts"
]
}
<project_root>/tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}
Oh no, and now all of a sudden it's making js files out of ts in my src folder!
When I delete all these extra js files (which, in the src folder, generate warnings in the running app), I also get these kind of warnings from WebStorm:
Oops! Something went wrong! :( ESLint: 6.7.2. ESLint couldn't find the plugin "eslint-plugin-import". (The package "eslint-plugin-import" was not found when loaded as a Node module from the directory "/Users/tuzmacbookpro2017/dev/Sunrise/IVD".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following: npm install eslint-plugin-import#latest --save-dev The plugin "eslint-plugin-import" was referenced from the config file in "nginx/front_end/package.json ยป eslint-config-react-app". If you still can't figure out the problem, please stop by https://gitter.im/eslint/eslint to chat with the team.
The react server and the cypress server and runner all seem happy though.
Help! What's with these js files? (I know TS transpiles ts into js or whatever, but usually the files don't actually show up like this)
Please make sure that Recompile on changes is not enabled in Settings | Languages & Frameworks | TypeScript
You can pass to the configuration file
false value to the next fields:
fixturesFolder
integrationFolder
pluginsFile
etc...
(Or just for the fields you need of course.)
documentation:
https://docs.cypress.io/guides/references/configuration.html#Folders-Files
I've got a monorepo project structured in the following way
/web
/native
/tsconfig.json
tsconfig.json
My /native/tsconfig.json looks like this
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"jsx": "react-native"
}
}
But for some reason my .tsx files with some jsx in them return following error
[ts] Cannot use JSX unless the '--jsx' flag is provided. [17004]
When I typecheck using cli tsc, I don't get these errors.
You need to specify the folder like this in your tsconfig.
"include": [
"./src/ts/**/*" //Path to your source
],
"jsx": "preserve",
Typescript version in VsCode workspace and package.json are not equal.
To solve this problem in Vscode,
Open the tsx file. In right bottom corner you will find
2 click this button and choose change VS Code to use the workspace's version of Typescript.
The errors will disappear.
I'm running into a bit of a confusing issue where it seems that TypeScript is installing its own copy of React into it's own global cache (not sure what its called? assuming thats what it is) and referencing it in my project.
Specifically I end up with two references to React one that is located in the root of my project
C:\MyProject\node_modules\#types\react
and then another reference in
C:\Users\MyUserName\AppData\Local\Microsoft\TypeScript\3.0\node_modules\#types\react
How do I control and uninstall references that end up in the TypeScript local folder? What might I be doing in my project that could be causing this secondary reference?
Here is what my tsconfig.json file looks like:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"sourceMap": true,
"jsx": "react",
"lib": [ "es6", "dom" ],
"removeComments": true,
"typeRoots": [
"/Types/"
]
},
"compileOnSave": false,
"exclude": [
"/node_modules/",
"/bin",
"/obj"
]
}
The feature that is downloading the type declarations to C:\Users\MyUserName\AppData\Local\Microsoft\TypeScript\3.0\node_modules is called "Automatic Type Acquisition" and is intended for JavaScript projects. Since your project has a tsconfig.json file, Automatic Type Acquisition should be inactive and any previously downloaded files should not be used; if you have evidence that they are being used and are causing problems with your project, please update the question. Assuming you are using Visual Studio Code, you can disable Automatic Type Acquisition by setting the typescript.disableAutomaticTypeAcquisition setting to true.
These issues comes up depending on your IDE and is resolved in different ways
For Visual Studio 2019:
Go to Options-> Text Editor -> JavaScript/TypeScript-> Project and Uncheck TypeAcquisition for Javascript
Close VisualStudio
Delete the Folder under C:\Users\ [Username]\AppData\Local\Microsoft\TypeScript\ [VersionOfConcern]
Reload Solution and your errors should be gone
VS Code Users should be refer to the tsconfig.json file option:
{
[...],
"typeAcquisition": {"enable": false}
}
I did not test the Solution for VS Code.
I have a tsconfig.json file with the following:
{"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "./target/src/",
"rootDir": "./src/main/webapp/",
"sourceMap": true,
"experimentalDecorators": true
},
"exclude": ["node_modules", "target"]
}
When I run tsc I get the error:
error TS6059: File '../node_modules/ng2-select/components/ng2-select-config.ts' is not under 'rootDir' 'main/webapp'. 'rootDir' is expected to contain all source files.
The culprit seems to be when I try to load ng2-select in a file:
import {Component} from 'angular2/core';
import {select} from 'ng2-select';
If I run tsc without the second import it's fine. If I add the second I get that error.
Any ideas as to why it's trying to compile ng2-select even though it's supposed to be ignored?
Thanks!
This is either an issue with the structure of the ng2-select module or a bug with the typescript compiler.
The issue is caused by having .ts and .d.ts files in the same directory as each other within the module directory that you are trying to load from. This seems to cause the typescript compiler to recompile the module then getting confused because it compiled something outside of the rootDir.
I have had the same issue and have raised it on the module's GitHub repository. This answer may help you for now though it isn't ideal. Hopefully there'll be a fix soon.
Update
The latest release fixes this issue.
So most examples I found on importing jspm packages to typescript assumed that I wanted to use Systemjs to load and interpret them in the browser. However, I would rather like to use tsc to build commonjs modules and only import the js code, since it seems to me to be the more general and error-resistent approach to me.
So my directory structure looks like this:
src/index.ts
jspm_packages/...
config.js
tsconfig.json
With tsconfig having the following content:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"noEmitOnError": true,
"noImplicitAny": false,
"rootDir": "src",
"outDir": "target/app",
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": true
},
"exclude": [
"jspm_packages",
"node_modules",
"typings",
"target"
]
}
For testing purposes, I installed angular 2 with jspm install npm:angular2 and tried to import it in my index.ts via import { bootstrap } from 'angular2/platform/browser';
When running tsc, I get the error
src/index.ts(1,27): error TS2307: Cannot find module 'angular2/platform/browser'.
Now I wonder, can I make jspm-packages known to typescript? I feel like I tried it all, removing the jspm_packages from the tsconfig exclude list, switching to node module resolution or systemjs module generation. Maybe I just haven't found the right combination. Any hints on what to try next?
I am also struggling with this same issue and after some research, I learned that there no good solutions to allow this as of yet.
Workarounds:
A) You can duplicate your dependencies and install it with npm. tsc should not throw any errors since its resolving from npm.
B) Change your tsconfig.json and map angular to the jspm path. For example
"baseUrl": ".",
"paths": {
"angular2/*": [
"jspm_packages/npm/angular2#2.0.0-beta.7/*"
],
"rxjs/*": [
"jspm_packages/npm/rxjs#5.0.0-beta.2/*"
]
}
See https://github.com/frankwallis/plugin-typescript/tree/master/examples/angular2 for a full example.
Note that "baseUrl" and "paths" are not official properties and only on the beta version of typescript compiler.
The issue is currently being tracked here:
https://github.com/Microsoft/TypeScript/issues/6012