TypeScript issue: textAlign
See original GitHub issueemotion
version: 10.0.6react
version: 16.6.3
Relevant code:
const Test = styled("div")(() => {
const attributes = {
textAlign: "center",
};
return {
...attributes,
};
});
What you did:
tried to assign textAlign
to a styled component. This is unique to textAlign
. Also if I set textAlign directly (not using the spread operator) everything works just fine:
const Test = styled("div")(() => {
return {
textAlign: "center",
};
});
What happened: I receive a TypeScript compile error (not only linting):
Type error: Argument of type '() => { textAlign: string; }' is not assignable to parameter of type 'TemplateStringsArray'.
Type '() => { textAlign: string; }' is missing the following properties from type 'TemplateStringsArray': raw, concat, join, slice, and 15 more. TS2345
Reproduction:
const Test = styled("div")(() => {
const attributes = {
textAlign: "center",
};
return {
...attributes,
};
});
Problem description: The described code seems to be valid TypeScript Code, but for some reason this is not compiled. If I skip TypeScript checks, the code is compiled just fine.
Suggested solution: ?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:7 (4 by maintainers)
Top Results From Across the Web
TypeScript type inference issue with string literal
Normally TypeScript would type textAlign as string, but since it can't just be any string, you can cast it to the more specific...
Read more >text-align - CSS: Cascading Style Sheets - MDN Web Docs
The text-align CSS property sets the horizontal alignment of the inline-level content inside a block element or table-cell box.
Read more >HTML DOM Style textAlign Property - W3Schools
Definition and Usage. The textAlign property sets or returns the horizontal alignment of text in a block level element. Browser Support. Property.
Read more >Type 'string' is not assignable to type 'TextAlign' - YouTube
... nextjs error | Type 'string' is not assignable to type ' TextAlign ' ... property style does not exist on type Element...
Read more >TextAlign – Tiptap Editor
This extension adds a text align attribute to a specified list of nodes. The attribute is used to align the text. Firefox bug....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think what’s going on here is the
textAlign
property ofattributes
is being inferred asstring
, but thetextAlign
property in csstype is a union of string literals, whichstring
is not assignable to.It should infer correctly if you provide a type annotation:
Alternatively, once https://github.com/Microsoft/TypeScript/pull/29510 is available then the following should also work:
This is a “limitation” of TS - described by several people in this thread. You just have to “narrow” down the type of those object properties to things actually accepted by CSS (string is just too broad of a type for this). For this you can use in an example const assertions, assigning object to a
CSS.Properties
etc.