SF Symbols for TypeScript

2.2.0 · active · verified Sun Apr 19

sf-symbols-typescript is a utility library providing TypeScript type definitions for Apple's SF Symbols. It enables developers to use SF Symbol names as string literal types, enhancing type safety in applications that consume these icons, particularly useful in environments like React Native. The current stable version is 2.2.0. The library primarily updates its types in sync with new releases of SF Symbols from Apple, incorporating new icons and symbol versions. Its key differentiators include having zero runtime dependencies and zero runtime code, making it a pure type-level utility. It also offers advanced features for restricting the available symbols globally via declaration merging or individually by importing specific version types, which is crucial for maintaining compatibility with target iOS/macOS versions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `SFSymbol` for type safety, global restriction via declaration merging, and importing version-specific symbol types.

import type { SFSymbol } from 'sf-symbols-typescript';

// Basic usage: type-checking SF Symbol names
const basicIcon: SFSymbol = 'arrow.up';
// const invalidIcon: SFSymbol = 'non.existent.symbol'; // This would cause a TypeScript error

// Globally restricting SF Symbol versions using declaration merging
declare module 'sf-symbols-typescript' {
  interface Overrides {
    // Restrict symbols to those found in SF Symbols 5.0 (e.g., for iOS 17.0+)
    SFSymbolsVersion: '5.0';
  }
}

// After declaration merging, SFSymbol now only includes 5.0 symbols.
// This ensures your app only uses symbols available on its minimum target OS.
const restrictedIcon: SFSymbol = 'faceid'; // 'faceid' is available in 5.0
// const newerIcon: SFSymbol = 'globe.desk'; // If 'globe.desk' was introduced in 6.0, this would now be a type error

// Alternatively, importing specific version types for granular control
import type { SFSymbols6_0 } from 'sf-symbols-typescript';
const specificVersionIcon: SFSymbols6_0 = 'globe.desk'; // Explicitly use a 6.0 symbol type

console.log(`Using basic icon: ${basicIcon}`);
console.log(`Using restricted icon: ${restrictedIcon}`);
console.log(`Using specific version icon: ${specificVersionIcon}`);

// This library has no runtime code, so these lines just demonstrate type usage.
// In a real app, you would pass these strings to a native UI component.

view raw JSON →