为什么TypeScript会跟踪函数静态属性的突变?
我一直认为 TypeScript 不会跟踪对象突变。例如:
type DescribableObject = {
name: string;
age: number;
};
// error
const obj: DescribableObject = {
name: 'sdf'
}
obj.age = 2
但是,似乎在某些情况下它会跟踪函数静态属性的突变。
type DescribableFunction = {
description: string;
(): boolean;
};
// error
const fn: DescribableFunction = () => true
//fn.description = 'hello';
如果取消注释//fn.description = 'hello';,TypeScript 错误将消失。
此外,如果您将鼠标悬停在上面,fn您会看到 TS 将其fn视为某种module.
fn函数是一个什么样的模块?这种行为有记录吗?