用来约定对象有哪些成员,而成员的类型是什么样的

  1. interface Post {
  2. title: string;
  3. content: string;
  4. }
  5. function printPost(post: Post) {
  6. console.log(post.title);
  7. console.log(post.content);
  8. }

可选成员

加上 ? 号

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 不是固定的,可以用其它名称代替