Skip to content

TypeScript 支持

🌐 TypeScript Support

@lucide/astro 包导出的类型列表。 在 TypeScript Astro 项目中使用 Lucide 图标时,可以使用这些类型来为你的组件定义类型。

🌐 List of exported types from the @lucide/astro package. These can be used to type your components when using Lucide icons in a TypeScript Astro project.

LucideProps

导出可以传递给图标组件的所有属性以及任何其他 SVG 属性,参见:MDN 上的 SVG 表现属性

🌐 Exports all props that can be passed to an icon component and any other SVG attributes, see: SVG Presentation Attributes on MDN.

ts
interface LucideProps extends SVGAttributes<SVGSVGElement> {
  name?: string;
  color?: string;
  size?: number | string;
  'stroke-width'?: number | string;
  absoluteStrokeWidth?: boolean;
  [key: string]: any; // Any other SVG attributes
}

使用 IconProps

🌐 Using IconProps

你可以使用 IconProps 接口为你的自定义图标组件输入属性类型。

🌐 You can use the IconProps interface to type props for your custom icon components.

astro
---
import { icons, type IconProps } from '@lucide/astro';

interface Props extends IconProps {
  name: keyof typeof icons;
}

const { name, ...restProps } = Astro.props;
const Icon = icons[name];
---

<Icon {...restProps} />

LucideIcon

单个图标组件的类型,当你想为一个保存图标组件的变量或属性定义类型时,这非常有用。

🌐 Type for individual icon components, this is use full when you want to type a variable or prop that holds an icon component.

ts
import type { Component } from 'astro/types';
import type { IconProps } from '@lucide/astro';

type LucideIcon = Component<IconProps>

使用 LucideIcon

🌐 Using LucideIcon

当你需要直接使用图标组件时,可以使用 LucideIcon 类型。

🌐 You can use the LucideIcon type when you need to work with icon components directly.

astro
---
import { House, Library, Cog, type Icon as IconType } from '@lucide/astro';

type MenuItem = {
  name: string;
  href: string;
  icon: typeof IconType;
};

const menuItems: MenuItem[] = [
  {
    name: 'Home',
    href: '/',
    icon: House,
  },
  {
    name: 'Blog',
    href: '/blog',
    icon: Library,
  },
  {
    name: 'Projects',
    href: '/projects',
    icon: Cog,
  },
];
---

{
  menuItems.map((item) => (
    <a href={item.href}>
      <item.icon />
      <span>{item.name}</span>
    </a>
  ))
}

IconNode

用于图标原始 SVG 结构的类型。这是一个包含 SVG 元素及其属性的数组,用于渲染图标。通常不直接在应用代码中使用,但对于高级用例可能很有用,例如使用自定义图标或与 Lucide Lab 一起使用。

🌐 Type for the raw SVG structure of an icon. This is an array of SVG elements and their attributes to render the icon. Not commonly used directly in application code. But can be useful for advanced use cases, such as using custom icons or with Lucide Lab.

ts
type IconNode = [
  elementName: 'circle' | 'ellipse'| 'g' | 'line' | 'path' | 'polygon' | 'polyline' | 'rect',
  attrs: HTMLAttributes<'svg'>
][];

使用 IconNode

🌐 Using IconNode

当你需要处理图标的原始 SVG 结构时,你可以使用 IconNode 类型。

🌐 You can use the IconNode type when you need to work with the raw SVG structure of an icon.

CustomIcon.astro
astro
---
import { type IconNode, Icon } from '@lucide/astro';

const customIcon: IconNode = [
  ['circle', { cx: 12, cy: 12, r: 10 }],
  ['line', { x1: 12, y1: 8, x2: 12, y2: 12 }],
  ['line', { x1: 12, y1: 16, x2: 12, y2: 16 }],
];
---

<Icon iconNode={customIcon} size="24" color="blue" />