You cannot directly import CSS or any other static files into typescript using only the typescript compiler, but you can with the help of some build tools...
For example using webpack, you can set up the css-loader and style-loader to search your source code for require('./whatever.css') and add it to the build before safely compiling your typescript. If you also have webpack generate your HTML then your CSS will be automatically injected as a stylesheet.
You cannot directly import CSS or any other static files into typescript using only the typescript compiler, but you can with the help of some build tools...
For example using webpack, you can set up the css-loader and style-loader to search your source code for require('./whatever.css') and add it to the build before safely compiling your typescript. If you also have webpack generate your HTML then your CSS will be automatically injected as a stylesheet.
See this answer:
https://stackoverflow.com/a/37144690/3850405
If you only need css for a class in your component you could do it like below. I like this solution for when inline css does not work and only small changes are needed.
import * as React from "react";
import { Header } from "./header";
export class Home extends React.Component<{}, {}> {
render() {
const css = `
.landing-page {
background-color: orange;
}
`
return (
<div>
<style>
{css}
</style>
<div id="page-top" className="landing-page">
<Header />
</div>
</div>
);
}
}
You cannot directly import CSS or any other static files into typescript using only the typescript compiler, but you can with the help of some build tools...
For example using webpack, you can set up the css-loader and style-loader to search your source code for require('./whatever.css') and add it to the build before safely compiling your typescript. If you also have webpack generate your HTML then your CSS will be automatically injected as a stylesheet.
See this answer:
https://stackoverflow.com/a/37144690/3850405
If you only need css for a class in your component you could do it like below. I like this solution for when inline css does not work and only small changes are needed.
import * as React from "react";
import { Header } from "./header";
export class Home extends React.Component<{}, {}> {
render() {
const css = `
.landing-page {
background-color: orange;
}
`
return (
<div>
<style>
{css}
</style>
<div id="page-top" className="landing-page">
<Header />
</div>
</div>
);
}
}
reactjs - How to write CSS / SCSS / PostCSS inside React .tsx file as JavaScript String in VSCode and WebStorm? - Stack Overflow
css module/ importing css files in tsx or ts files
Is there a solution for CSS modules that would allow to have TSX and CSS in the same file?
reactjs - Syntax Highlighting for CSS in a Style Tag in a TSX Component - Stack Overflow
Videos
WebStorm has a built-in feature Language Injection. for that.
Here you will find in detail documentation of that feature.
Short version.
- Place your cursor in the relevant code block and click
alt + enter.

- In the DropDown, select
Inject language or reference. Without clicking to the right, clickenteragain.

- A window should open, now select the language you like to inject. If you select
CSSthe result should be:

To answer your second question. It shouldn't cause any trouble down the road.
edit addition:
It is possible to trigger the same behaviour with a comment laguange=css

In vscode you can use ES6 String css vscode extension
Just add /*css*/ before ` like this
function getStyle() {
return /*css*/`
h1 {
color: blue;
}
`
}
I said "dedicated style file" instead of stylesheet since we have more than just CSS to apply styles nowadays. Emotion and MUI's sx props, Tailwind's CSS classes, styled-components styled function, and much more.
There are instances I saw that some people prefer keeping a long list of class names or sx props in the TSX file, which I'm not sure whether is a good idea in the long run since it sounds like it will affect readability, especially when conditional rendering comes into play.
While separating them also has concerns as well because now I need to have at least two files opened to have a full picture of what the component is about.
What do you think?