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 OverflowReact Tsx with React Bootstrap - Inline CSS for Active NavItem - Stack Overflow
Allow inline styles in TypeScript
css - How can I implement an error-free dynamic in-line style in react with typescript? - Stack Overflow
Add the inline css in Angular 7 from component.ts - Stack Overflow
Videos
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>
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, becausecoloraccepts values of type string. fontWeight: 'bold'doesn't work, althoughfontWeightaccepts"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>
You are not using the props as far as I can see and besides that props are immutable. You might want to look into state and use that as the backGroundColor but for inline styling it could look like this
<div style={{color: condition ? "red": "green"}}> </div>
EDIT: Seems like there is no styling intended for the NavItem. See here. You have to use css for that e.g. Change the <a> element class in react-bootstrap NavItem
Solved it like this, not the prettiest solution but works fine.
const css = `
.route-list .nav-tabs>li.active>a {
background-color: green;
color: white;
}
`
<main>
<div className="route-list">
<style>{css}</style>
<Nav bsStyle="tabs" activeKey="1">
<LinkContainer to="/about">
<NavItem eventKey="1">Om</NavItem>
</LinkContainer>
<LinkContainer to="/facts">
<NavItem eventKey="2">Fakta</NavItem>
</LinkContainer>
</Nav>
</div>
</main>
» npm install @types/inline-css
What about using ngStyle
customeStyle =
{
'color': 'red'
}
;
<p [ngStyle]="customeStyle">asdasd</p>
Instead of querying the DOM, you can use Angular Style binding
Template
<h1 [style.color]="isHoussein() ? 'red': 'black'">{{firstname}}</h1>
Name: <input type="text" [(ngModel)]="firstname">
<button (click)="changeName()">Change Name</button>
Component.ts
export class MyApp {
firstname: string = 'Jimmy';
changeName() {
this.firstname = 'Houssein';
}
isHoussein() {
return this.firstname === 'Houssein';
}
}