结构类型(structural typing):一种类型系统判定方式,类型是否兼容主要看它们的结构(如字段、方法的名称与类型)是否匹配,而不是看它们是否显式声明为同一“名义类型”(如同一个类/接口名)。常见于一些面向对象或函数式语言及其类型系统中。(也常与 nominal typing“名义类型”对比。)
/ˈstrʌktʃərəl ˈtaɪpɪŋ/
A language with structural typing lets you use any object that has the required fields.
结构类型的语言允许你使用任何具备所需字段的对象。
In a structural type system, a value can satisfy an interface without explicitly declaring it, as long as its shape matches the interface’s methods and properties.
在结构类型系统中,只要一个值的“形状”(方法与属性)与接口要求一致,它就可以满足该接口,即使它没有显式声明实现该接口。
structural 来自拉丁语 structura(“建造、结构”),强调“构成方式/结构”。typing 源自 type(“类型、范式”),在计算机语境中指“类型判定/类型系统”。合起来表示“按结构来判断类型”的方法。