Skip to content

TypeScript

All flag components are fully typed with TypeScript, providing autocomplete and type safety out of the box.

All flag components accept standard React SVG props:

import type { SVGProps } from 'react'
import { FlagUs } from '@sankyu/react-circle-flags'
interface Props {
flagProps?: SVGProps<SVGSVGElement>
}
export function FlagItem({ flagProps }: Props) {
return <FlagUs {...flagProps} />
}
import type { SVGProps, ComponentProps } from 'react'
import { FlagCn } from '@sankyu/react-circle-flags'
// Method 1: Using SVGProps
type FlagProps1 = SVGProps<SVGSVGElement>
// Method 2: Using ComponentProps (recommended)
type FlagProps2 = ComponentProps<typeof FlagCn>
export function Flag(props: FlagProps2) {
return <FlagCn {...props} />
}

The library exports helpful types for advanced use cases:

import { coerceFlagCode, isFlagCode } from '@sankyu/react-circle-flags'
import type { CountryCode, FlagCode, FlagComponentProps } from '@sankyu/react-circle-flags'
// FlagCode: Union type of all available flag codes
const validCode: FlagCode = 'us' // ✓ Valid
const invalidCode: FlagCode = 'invalid' // ✗ Type error
// CountryCode: a semantic alias of FlagCode (same union)
const alsoValid: CountryCode = validCode
// FlagComponentProps: Standard props for flag components
const props: FlagComponentProps = {
width: 64,
height: 64,
className: 'my-flag',
}
// Runtime data narrowing/coercion helpers
const fromApi: string = 'US'
const safeCode = coerceFlagCode(fromApi) // -> FlagCode (unknown values become "xx")
const isNormalizedValid = isFlagCode('us') // true (expects already-normalized input)
import { FlagUtils } from '@sankyu/react-circle-flags'
// Convert country code to component name (PascalCase)
FlagUtils.getComponentName('us') // 'FlagUs'
FlagUtils.getComponentName('gb-eng') // 'FlagGbEng'
// Normalize display format (uppercase)
FlagUtils.formatCountryCode('us') // 'US'

The FlagUtils object provides convenient utility functions for working with flag codes:

import { FlagUtils } from '@sankyu/react-circle-flags'
// Validate country codes
FlagUtils.isValidCountryCode('us') // true
FlagUtils.isValidCountryCode('invalid') // false
// Format country codes
FlagUtils.formatCountryCode('us') // "US"
// Get component names
FlagUtils.getComponentName('us') // "FlagUs"
// Handle flag sizes
FlagUtils.getSizeName(48) // "lg"
FlagUtils.sizes.lg // 48
import { DynamicFlag } from '@sankyu/react-circle-flags'
import type { FlagCode } from '@sankyu/react-circle-flags'
interface CountrySelectProps {
selectedCountry: FlagCode
onSelect: (code: FlagCode) => void
}
export function CountrySelect({ selectedCountry, onSelect }: CountrySelectProps) {
return (
<div>
<DynamicFlag strict code={selectedCountry} width={32} height={32} />
<button onClick={() => onSelect('us')}>USA</button>
<button onClick={() => onSelect('cn')}>China</button>
</div>
)
}