---
title: "TypeScript magic"
description: "Unioning string literal types with `string` collapses the result to plain `string`, losing autocomplete."
url: "https://swe.quest/articles/typescript-magic-8cc97c800affc196"
canonical: "https://artsy.github.io/blog/2023/03/01/typescript-magic/"
source: "Artsy Engineering"
published: "2023-03-01"
difficulty: "medium"
kind: "How-to"
reading_minutes: 4
tags: ["typescript","type-systems","design-systems","react"]
---

# TypeScript magic

A post on [Artsy Engineering](https://swe.quest/sources/artsy.md), published 2023-03-01, 4 min read, How-to. Original: [https://artsy.github.io/blog/2023/03/01/typescript-magic/](https://artsy.github.io/blog/2023/03/01/typescript-magic/)

## Summary

Unioning string literal types with \`string\` collapses the result to plain \`string\`, losing autocomplete. Artsy hit this in its palette-mobile React Native design system, where colors should accept named DS tokens like \`black100\` but also any other color string. Intersecting with an empty object, \`string & {}\`, produces a type that still accepts any string but is not absorbed in the union. That keeps the literal members visible for editor autocomplete while leaving other values loose.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [TypeScript](https://swe.quest/tags/typescript.md) (Languages)
- [Type systems](https://swe.quest/tags/type-systems.md) (Compilers & runtimes)
- [Design systems](https://swe.quest/tags/design-systems.md) (Web & mobile)
- [React](https://swe.quest/tags/react.md) (Web & mobile)

## Prerequisites

- TypeScript union types ([easy posts on TypeScript](https://swe.quest/tags/typescript.md?difficulty=easy))
- string literal types

## Keywords

TypeScript, palette-mobile, React Native, template literal types

## Related posts

- [The shared language of props](https://swe.quest/articles/the-shared-language-of-props-ff2798f5935efff7.md): Figma · 2023-07-19 · Medium · 17 min read · Design systems, React, TypeScript, Accessibility, Frontend
  Figma component properties and codebase component props share names but serve different purposes: design props mark visual differences, while code props also carry behaviour, events and data.
- [How to Write Code Without Having to Read It](https://swe.quest/articles/how-to-write-code-without-having-to-read-it-1700fbc56278687d.md): Shopify Engineering · 2022-10-11 · Medium · 12 min read · Type systems, Testing, Refactoring & migrations, TypeScript, Developer productivity
  Engineers can safely change unfamiliar code with minimal reading by letting the compiler, linter and unit tests guide them: locate the entrypoint via user-visible strings (or git bisect for regressions), make the breaking change at the leaf component, then climb the callstack following build errors and sweep back down via lint errors, exhaustive switches and test failures.
- [Creating a React Library for Consistent Data Visualization](https://swe.quest/articles/creating-a-react-library-for-consistent-data-visualization-ab1526a6f8337781.md): Shopify Engineering · 2022-03-15 · Medium · 6 min read · Design systems, React, Frontend, API design
  Shopify's Polaris Viz React charting library replaced scattered per-chart style props with a single \`theme\` prop backed by a typed Theme record, removing visual inconsistencies across the admin.
- [A Gentle Intro to TypeScript](https://swe.quest/articles/a-gentle-intro-to-typescript-acf96e72b2375e24.md): Deno · 2024-06-27 · Medium · 5 min read · TypeScript, Type systems, JavaScript
  TypeScript catches the bugs that JavaScript's dynamic typing and implicit type coercion allow, such as add(1, "2") returning "12", by reporting them at compile time instead of at runtime.
- [Theming the Zalando Design System](https://swe.quest/articles/theming-the-zalando-design-system-b14098d887a4b230.md): Zalando Engineering · 2024-05-13 · Medium · 20 min read · Design systems, Frontend, CSS, Mobile
  Zalando built theming into its design system by extracting repeated values into semantic design tokens (e.g. \`accent\` instead of \`orange\`) and adding component-level tokens that reference global ones, so themes can change a button's padding without redefining global scales.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
