SF Symbols for TypeScript
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
-
Error: "SFSymbol" cannot be used as a value because it was imported using 'import type'.
cause Attempting to use `SFSymbol` or other type imports as if they were runtime values (e.g., `console.log(SFSymbol)` or passing it to a functionExpectingAValue).fixRemember that `SFSymbol` is a TypeScript type only. It has no runtime representation. It should only be used in type annotations or declarations, such as `const icon: SFSymbol = '...'`. -
Property 'SFSymbolsVersion' does not exist on type 'Overrides'.
cause This error occurs if you're trying to directly modify the `Overrides` interface without using a `declare module` block, or if your `declare module` block for `Overrides` is syntactically incorrect.fixEnsure you are using declaration merging correctly: `declare module 'sf-symbols-typescript' { interface Overrides { SFSymbolsVersion: 'X.Y' } }` in a global `.d.ts` file. Do not try to import or extend `Overrides` as a regular interface. -
Type '"non.existent.symbol"' is not assignable to type 'SFSymbol'.
cause This error means the string literal you provided does not match any of the valid SF Symbol names defined by the `SFSymbol` type. This can happen if you made a typo, or if you restricted the `SFSymbol` type to an older version of SF Symbols and are trying to use a newer symbol.fixDouble-check the symbol name for typos. If you've restricted the `SFSymbol` type to an older version (e.g., '5.0'), ensure the symbol you're using is available in that version. Consult the SF Symbols app or the Version Support Table in the package's README to verify symbol availability for your chosen version.
Warnings
- breaking Version 2 introduced a new mechanism for specifying SF Symbols compatibility through declaration merging with the `Overrides` interface. This replaces any previous, less flexible methods of version restriction.
- gotcha The package provides only TypeScript types. There is no JavaScript runtime code or default export. Attempting a standard `import SFSymbol from 'sf-symbols-typescript'` or `require('sf-symbols-typescript')` will result in runtime or build errors.
- gotcha When restricting symbols globally via `Overrides` interface declaration merging, ensure the `declare module` block is in a global declaration file (e.g., `src/globals.d.ts` or `src/types.d.ts`) that is included in your `tsconfig.json`. Placing it directly in a `.ts` or `.tsx` file that's part of your main application logic might not apply it globally.
Install
-
npm install sf-symbols-typescript -
yarn add sf-symbols-typescript -
pnpm add sf-symbols-typescript
Imports
- SFSymbol
import { SFSymbol } from 'sf-symbols-typescript'import type { SFSymbol } from 'sf-symbols-typescript' - SFSymbols{major}_{minor}
import { SFSymbols5_0 } from 'sf-symbols-typescript'import type { SFSymbols5_0 } from 'sf-symbols-typescript' - Overrides (declaration merging)
import { Overrides } from 'sf-symbols-typescript'declare module 'sf-symbols-typescript' { interface Overrides { SFSymbolsVersion: '6.0' } }
Quickstart
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.