Type Definitions
TypeScript interfaces and types available in the library
Country
Represents a country with all its properties and cities.
interface Country {
code: string; // ISO 3166-1 alpha-2 code (e.g., "EG", "SA")
name: string; // English name
nameAr: string; // Arabic name (العربية)
nameFr: string; // French name (Français)
cities: City[]; // Array of cities/states/provinces
}{
code: "EG",
name: "Egypt",
nameAr: "مصر",
nameFr: "Égypte",
cities: [
{ name: "Cairo", nameAr: "القاهرة", nameFr: "Le Caire" },
{ name: "Alexandria", nameAr: "الإسكندرية", nameFr: "Alexandrie" },
// ... 25 more governorates
]
}City
Represents a city, state, province, or governorate.
interface City {
name: string; // English name
nameAr: string; // Arabic name (العربية)
nameFr: string; // French name (Français)
}{
name: "Riyadh",
nameAr: "الرياض",
nameFr: "Riyad"
}Language
Supported languages for search and display.
type Language = 'en' | 'ar' | 'fr';
// Usage in functions
searchCountries(query: string, lang?: Language): Country[]
getCountryName(code: string, lang?: Language): string | undefinedSearchResult
Returned when searching for cities globally.
interface SearchResult {
city: City;
country: Country;
}
// Returned by searchCities function
searchCities(
query: string,
countryCode?: string,
lang?: Language
): SearchResult[]TypeScript Benefits
- • Full IntelliSense support in VS Code
- • Compile-time type checking
- • Auto-completion for all properties
- • Better refactoring capabilities
- • Self-documenting code
Usage with TypeScript
import type { Country, City, Language } from 'countries-cities-ar';
// Type-safe function
function formatCountryName(country: Country, lang: Language = 'en'): string {
switch (lang) {
case 'ar': return country.nameAr;
case 'fr': return country.nameFr;
default: return country.name;
}
}
// Type-safe component props
interface Props {
country: Country;
onCitySelect?: (city: City) => void;
}
function CountryCard({ country, onCitySelect }: Props) {
// Component implementation
}