这个报错意味着你在代码中将一个变量名当作类型名使用了,但是 TypeScript 认为这是一个值。要解决这个问题,需要将变量名改为对应的类型名。如果要使用这个变量,可以使用 typeof 来获得对应的类型。例如:
class Post {
title: string;
content: string;
}
function printPost(post: Post) {
console.log(post.title);
console.log(post.content);
}
const myPost = new Post();
myPost.title = 'TypeScript';
myPost.content = 'Cool';
printPost(myPost);
// 错误示例,将变量名当作类型名使用
const post: Post = {
title: 'TypeScript',
content: 'Cool',
};
// 正确示例,使用 typeof 获得类型
const post: typeof Post = {
title: 'TypeScript',
content: 'Cool',
};