Skip to content

结构输入

一句话解释

结构输入是 类型系统哪里 compatibility 是 determined 通过结构 (属性) 的类型, 不通过它们的 names or declarations.

用大白话说

Structural typing means "如果它 looks 如 duck and quacks 如 duck, 它's duck." TypeScript checks the shape of a type — its properties and their types — to determine compatibility, not the type's name.

This is different from nominal typing (used in Java, C#) where types are only compatible if they have an explicit inheritance relationship. In TypeScript, any object with the right properties is compatible, even if it was never declared as the target type.

Structural typing makes TypeScript flexible and JavaScript-like, but it can lead to surprising compatibilities. It is a core design principle of the language.

示例

typescript
interface Point { x: number; y: number; }

// No explicit relationship to Point
const coord = { x: 10, y: 20, label: "origin" };

const p: Point = coord; // OK: coord has x and y

coord 是 compatible 使用点 因为它 有 x and y 属性, despite 否 declared 关系.

相关术语

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表