question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript issue: textAlign

See original GitHub issue
  • emotion version: 10.0.6
  • react 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

image

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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
cameron-martincommented, Feb 5, 2019

I think what’s going on here is the textAlign property of attributes is being inferred as string, but the textAlign property in csstype is a union of string literals, which string is not assignable to.

It should infer correctly if you provide a type annotation:

import * as CSS from 'csstype';

const Test = styled("div")(() => {
  const attributes: CSS.Properties = {
    textAlign: "center",
  };
  return {
    ...attributes,
  };
});

Alternatively, once https://github.com/Microsoft/TypeScript/pull/29510 is available then the following should also work:

const Test = styled("div")(() => {
  const attributes = {
    textAlign: "center",
  } as const;
  return {
    ...attributes,
  };
});
0reactions
Andaristcommented, Nov 4, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found