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.

tw`bg-white` at use tw variable with template string it is not work!

See original GitHub issue

I get an error: Argument of type 'TemplateStringsArray' is not assignable to parameter of type 'StringLike'. Type 'TemplateStringsArray' is not assignable to type 'string'.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
sastancommented, May 5, 2022

@KyleJune You could use the style helper which provides a stitches like API. Docs are missing but here are the tests: https://github.com/tw-in-js/twind/blob/next/packages/twind/src/tests/style.test.ts#L61

import { style } from 'twind'

const navLinkStyle = style({
  label: 'NavLink', // optional
  // the classes to always use
  base: `px-3 py-2 rounded-md text-sm font-medium`,
  // only needed in cases there is no isActive in the props
  defaults: {
    isActive: false,
  },
  // define the props the navLinkStyle function accepts
  props: {
    isActive: {
      true: `bg-gray-900 text-white`,
      false:  `text-gray-300 hover:bg-gray-700 hover:text-white`,
    },
  },
})

// Usage
function Component({ item }) {
  return (<NavLink
    key={item.name}
    to={item.to}
    end
    className={navLinkStyle}
    // or explicit
    className={({ isActive }) => navLinkStyle({ isActive })}
  >
    {item.name}
  </NavLink>)
1reaction
sastancommented, May 4, 2022

In v0.16, tw took a template string.

twind.dev/api/interfaces/twind.tw.html#callable

The documentation for v1 migration says the following. It makes it sound like we can use it the same as we were in v0.16.

tw as known from twind v0.16

twind.style/docs/migration

The readme on GitHub says the same.

https://github.com/tw-in-js/twind/blob/next/website/pages/docs/migration.md#notable-changes

You are right that’s a mistake in the docs.

It looks like the equivalent of v0.16 tw is tx.bind(tw). That’s not clear from the documentation. I found out about tx from the following reference page but it just says that it is shorthand for tw(cx(...args)). Coming from v0.16, it wasn’t clear what cx does, so I didn’t realize tx would get me the same behavior. I didn’t make the connection until reading your comment on this issue.

twind.style/docs/reference#utilities

I think it would help if migration docs were updated to reflect that tx is the equivalent of v0.16 tw and that v1 tw is different.

Yep. I must adjust this.

Any insight into why the call signature of tw was changed in V1?

The main reason is performance. Interpolating a template string array takes some time. By accepting only a string we can boost the performance for many cases (like transforming the class name attribute).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
template strings not working [duplicate] - Stack Overflow
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with ...
Read more >
Understanding Template Literals in JavaScript - DigitalOcean
In this article, you will go over the differences between single/double-quoted strings and template literals, running through the various ways ...
Read more >
Getting Literal With ES6 Template Strings - Chrome Developers
Template Strings use back-ticks (``) rather than the single or double quotes ... the addition of variables) and inside a Template Literal, ...
Read more >
Documentation - Template Literal Types - TypeScript
Generating mapping types which change properties via template literal strings.
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