Skip to content

Broken value-object toString interpolation in v6 #5740

Closed

Description

@FDufBos

Summary

Plain Objects with a custom own toString() method are not interpolated correctly in tagged template literals. Instead of calling toString(), styled-components treats them as CSS-in-JS styled objects via the isPlainObject check in flatten(), producing broken CSS output.

I noticed this was previously reported in issue 4312 but was closed as stale.

Why I'm hitting this issue

I'm building a design token system where token groups carry sub-values as properties (e.g. default, hovered, pressed). I've defined toString() to resolve to the default value so consumers can write theme.color.text instead of needing to write the full theme.color.text.default.

Reproduction

const token = {
	default: `#000000`,
	subtle: `#AAAAAA`,
	toString() { return  '#000000'};
};

const Text = styled.p`
	color: $ {token};
`

Expected behavior: toString() gets called on the value object and returns color: #000000 in the example.

Current behavior: isPlainObject returns true, so flatten() routes the object into objToCssArrayInto, which iterates every key (default, subtle, toString) and emits them as CSS property-value pairs.

Cause

I've been exploring flatten.ts, it seems like the isPlainObject checruns before the toString() fallback. Since object literals have constructor.name === 'Object' they are always caught by isPlainObject and never reach the final branch that calls .toString()

Fix: Could this be fixed by checking for a custom toString before checking isPlainObject with hasOwnProperty('toString')?

Activity

  1. self-assigned this
    on May 14, 2026
  2. quantizor commented on May 14, 2026

    @quantizor
    Contributor

    Could you try [email protected] when you get a chance?

  3. quantizor commented on May 19, 2026

    @quantizor
    Contributor

    Fix released with 6.4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions