用来约定对象有哪些成员,而成员的类型是什么样的
interface Post {
title: string;
content: string;
}
function printPost(post: Post) {
console.log(post.title);
console.log(post.content);
}
可选成员
加上 ? 号
interface Post {
title: string;
content: string;
subtitle?: string;
}
只读成员
加下 readonly 修饰符
interface Post {
title: string;
content: string;
subtitle?: string;
readonly summary: string;
}
动态成员
interface Cache {
[key: string]: string;
}
const cache: Cache = {};
cache.foo = 'value1';
cache.bar = 'value2';
key 不是固定的,可以用其它名称代替