Typescript can be awfuly silly sometimes. It's inferring the type of fontWeight as a string, even though it could narrow it to its exact literal. You can just cast it as itself to fix this:

const boldText = {
    fontWeight: 'bold' as 'bold'
}

<td style={boldText}>Content</td>

These days you can also use the new as const declaration at the end of your literals for the same effect:

const boldText = {
    fontWeight: 'bold'
} as const;

<td style={boldText}>Content</td>

And finally, you can always provide an explicit type to the object when you declare it:

const boldText: React.CSSProperties = {
    fontWeight: "bold"
};

<td style={boldText}>Content</td>
Answer from CRice on Stack Overflow
🌐
ReactHustle
reacthustle.com › blog › how-to-set-inline-styles-in-react-typescript
How to set inline styles in react (Typescript) | ReactHustle
To fix this we can use module augmentation or declaration merging to add more properties to the React.CSSProperties typescript definition. First create a global.d.ts in the root folder of your project and add the following code · import React from "react"; declare module "react" { // augment CSSProperties here interface CSSProperties { "--value"?: string | number; "--size"?: string | number; "--thickness"?: string | number; } } ... In this tutorial we learned how to add inline styles in react with typescript and also extend the inline style definition using module augmentation or declaration merging.
🌐
Medium
medium.com › @m.mondik › how-we-handle-inline-styles-with-typescript-and-react-2c257e039f2b
How we handle inline styles with TypeScript and React | by Michal Mondík | Medium
August 10, 2018 - import { PureComponent } from 'react'; import styleConstants from './styleConstants';export interface Colors { gray: string; primary: string; }export interface MediaQueries { max767: string; }export interface StyleConstants { colors: Colors; mediaQueries: MediaQueries; }export type StyleMap = any;export interface StyleCallback { (localStyle: StyleMap, globalStyle: StyleConstants): StyleMap; }export type Style = StyleCallback | StyleMap;export interface StyledComponentProps { style?: Style; }export interface InternalStyle { (styleConstants: StyleConstants): StyleMap; }export default class StyledComponent<P, S> extends PureComponent<P & StyledComponentProps, S> { getStyle(localStyle: InternalStyle): StyleMap { const computedLocalStyle = localStyle(styleConstants); const { style } = this.props as Readonly<StyledComponentProps>; return { ...computedLocalStyle, ...style as StyleMap }; } }
Discussions

React Tsx with React Bootstrap - Inline CSS for Active NavItem - Stack Overflow
I would like to change the background color for the active NavItem element to green using inline CSS. I'm using TypeScript 2.2, React, React Bootstrap, React Router Dom and React Router Bootstrap. Is More on stackoverflow.com
🌐 stackoverflow.com
Allow inline styles in TypeScript
Inline styles are also important for migrations from react-native / react-native-web code, as some projects use inline styles extensively. Simply using the above code in a minimal TypeScript project with react-strict-dom throws the above errors. More on github.com
🌐 github.com
1
March 25, 2024
css - How can I implement an error-free dynamic in-line style in react with typescript? - Stack Overflow
So after quite some searching, I finally found the answer. Basically, it is not possible to pass the style as a variable in Typescript. Instead, the style has to be applied directly inline. More on stackoverflow.com
🌐 stackoverflow.com
Add the inline css in Angular 7 from component.ts - Stack Overflow
My angular 7 application, I have the scrolling to see the products from left to right and right to left using this. Scrolling Each image has the button when ever it is clicked, I have applied the ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 5
54

Typescript can be awfuly silly sometimes. It's inferring the type of fontWeight as a string, even though it could narrow it to its exact literal. You can just cast it as itself to fix this:

const boldText = {
    fontWeight: 'bold' as 'bold'
}

<td style={boldText}>Content</td>

These days you can also use the new as const declaration at the end of your literals for the same effect:

const boldText = {
    fontWeight: 'bold'
} as const;

<td style={boldText}>Content</td>

And finally, you can always provide an explicit type to the object when you declare it:

const boldText: React.CSSProperties = {
    fontWeight: "bold"
};

<td style={boldText}>Content</td>
2 of 5
6

Typescript sees boldText as a "normal" object, and so infers boldText.fontWeight as string:

const boldText = {
    fontWeight: 'bold' // <-- TS doesn't know more than this is a string
}

The code <td style={boldText}>Content</td> expects CSSProperties, so a string can't be assigned.

To solve the mismatch, you can tell Typescript the type of the object:

const boldText: CSSProperties = {
    fontWeight: 'bold'
}

To be more precise:

  • e.g. color: '#ff0000' would work, because color accepts values of type string.
  • fontWeight: 'bold' doesn't work, although fontWeight accepts "bold", because TS decides to use the type string before it knows about the context of CSS, and the type string is not the same as the type "bold" | "bolder" | 100 | 200 | ...:
const boldText = {
    fontWeight: 'bold'  // <-- TS makes it's decision for type 'string'
    color: '#ff0000'    // <-- TS makes it's decision for type 'string'
}

// TS doesn't complain about color, as color accepts NamedColor | string | ...
// TS only complains about fontWeight, as it accepts only "bold" | "bolder" | 100 | 200 | ...
const TD = <td style={boldText}>Content</td>
🌐
npm
npmjs.com › package › @types › inline-css
@types/inline-css - npm
May 5, 2025 - TypeScript definitions for inline-css. Latest version: 3.0.4, last published: 7 months ago. Start using @types/inline-css in your project by running `npm i @types/inline-css`. There are 9 other projects in the npm registry using @types/inline-css.
      » npm install @types/inline-css
    
🌐
CodeSandbox
codesandbox.io › s › react-typescript-custom-inline-style-5meh2
React typescript custom inline style - CodeSandbox
September 15, 2021 - React typescript custom inline style by thinhlesdev using react, react-dom, react-scripts
Published   Sep 15, 2021
Author   thinhlesdev
🌐
DhiWise
dhiwise.com › post › how-to-elevate-ui-design-using-react-cssproperties-typescript
Implementing React CSSProperties Typescript for Styling
May 20, 2024 - TypeScript also assists in ensuring the 'style attribute' of elements like an h1 tag is of the correct type in editors like Visual Studio Code. Inline styles in React are applied using the style prop, which accepts an object where the keys are camelCased versions of the CSS property names, and the values are the CSS values as strings.
Find elsewhere
🌐
Webdevtutor
webdevtutor.net › blog › typescript-css-inline
Mastering Inline CSS in TypeScript
Inline CSS in TypeScript offers several advantages, including: Scoped Styling: Inline CSS allows you to apply styles directly to individual elements, ensuring that styles are scoped and do not affect other parts of your application.
🌐
GitHub
github.com › facebook › react-strict-dom › issues › 62
Allow inline styles in TypeScript · Issue #62 · facebook/react-strict-dom
March 25, 2024 - Types of property 'backgroundColor' are incompatible. Type 'string' is not assignable to type 'StyleXClassNameFor<"backgroundColor", Readonly<string | null | undefined>>'. Type 'string' is not assignable to type '{ _opaque: unique symbol; _key: "backgroundColor"; _value: Readonly<string | null | undefined>; }'.ts(2322) ... It's not very clear if inline styles are allowed in react-strict-dom from docs, but looking at the example in this repository, inline styles should be allowed.
Author   javascripter
🌐
GitHub
github.com › topics › inline-styles
inline-styles · GitHub Topics · GitHub
December 12, 2022 - 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. ... Concise way to write runtime inline CSS styles.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-inline-style
Mastering TypeScript Inline Styles in React
Dynamic Styling: Inline styles ... preventing global style conflicts. To start using inline styles in TypeScript, you can define styles directly in your component using the style attribute....
🌐
Stack Overflow
stackoverflow.com › questions › 72508657 › how-can-i-implement-an-error-free-dynamic-in-line-style-in-react-with-typescript
css - How can I implement an error-free dynamic in-line style in react with typescript? - Stack Overflow
So after quite some searching, I finally found the answer. Basically, it is not possible to pass the style as a variable in Typescript. Instead, the style has to be applied directly inline.
🌐
TypeScript
typescriptlang.org › play › javascript › external-apis › typescript-with-web.js.html
TypeScript: Playground Example - TypeScript with Web
const popover = document.createElement("div"); popover.id = "example-popover"; // Note that popover is correctly typed to be a HTMLDivElement specifically because we passed in "div". To make it possible to re-run this code, we'll first add a function to remove the popover if it was already there. const removePopover = () => { const existingPopover = document.getElementById(popover.id); if (existingPopover && existingPopover.parentElement) { existingPopover.parentElement.removeChild(existingPopover); } }; // Then call it right away. removePopover(); // We can set the inline styles on the element via the .style property on a HTMLElement - this is fully typed.
🌐
Oida
oida.dev › typescript-react › styles
TypeScript and React: Styles and CSS - oida.dev
July 17, 2019 - You don’t get any class names you can attach, but at least you can import styles: ... If you want to get all the class names, and really nice auto-completion, drop the ambient files and include another loader: css-modules-typescript-loader · Styling requires a bit of infrastructure. Here’s some demos to get you started. This Codesandbox has samples for inline styles, style imports, emotion and styled components.
🌐
xjavascript
xjavascript.com › blog › inline-typescript
Inline TypeScript: A Comprehensive Guide — xjavascript.com
Avoid writing large and complex TypeScript code inline as it can make the file hard to read and maintain. Type assertions should be used sparingly because they bypass TypeScript's type checking. Only use them when you are absolutely sure about the type. const someValue: unknown = "a string"; const strLength: number = (someValue as string).length; Adhere to a consistent coding style, such as naming conventions for variables, functions, and types.
🌐
Codersera
codersera.com › blog › complete-guide-to-inline-style-in-a-react-app
Complete Guide To Inline Style In A React App
March 31, 2023 - Also Read | React Hooks With Typescript : UseCallback, UseMemo, UseContext And UseRef · Need to write redundant CSS properties individually for each element. CSS properties will be limited to a component scope only, so there is no reusability · It’s impossible to style pseudo-elements, pseudo-classes, media queries, etc with inline styles.
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-add-css-style-to-element
Set CSS styles on an Element using TypeScript | bobbyhadz
February 29, 2024 - However, this wouldn't work if the property was not set as an inline style on the element. If you need to consider styles set in external stylesheets, use thewindow.getComputedStyle method instead. The getComputedStyle method returns an object that contains the values of all CSS properties of the passed-in element, after applying stylesheets. I've also written an article on how to add a class to an element in React. If you need to show/hide an element in TypeScript, check out the following article.
🌐
React
react.dev › learn › typescript
Using TypeScript – React
To get type-checking, you can use the TypeScript Playground or use a more fully-featured online sandbox. This inline syntax is the simplest way to provide types for a component, though once you start to have a few fields to describe it can become unwieldy. Instead, you can use an interface or type to describe the component’s props: