首页 > 解决方案 > const 变量声明是类型定义吗

问题描述

通过以下const变量(Point)的声明,

const Point = {
        x: 1,
        y: 2
}

Point.z = 6说,Property 'z' does not exist on type '{ x: number; y: number; }'

const变量声明是类型定义吗?

标签: typescriptconstantstype-inference

解决方案


不,TypeScript 会根据您将Point变量设置为的类型推断类型。const正在设置它,以便您无法重新分配Point变量。var您将在代码中使用or看到相同的行为let


推荐阅读