1. // 接口
    2. export = {}
    3. // 定义一个接口
    4. interface Post {
    5. title: string
    6. content: string
    7. subTitle?: string // 可选成员
    8. readonly summary: string // 只读成员
    9. }
    10. // 则这里就需要遵循接口的规则
    11. function printPost (post: Post) {
    12. console.log(post.title)
    13. console.log(post.content)
    14. }
    15. const hello: Post = {
    16. title: "title",
    17. content: "content",
    18. summary: "hello"
    19. }
    20. printPost(hello)
    21. // 只读成员声明后不能修改,这里会报出语法错误
    22. // hello.summary = '12'
    23. // ---------------------------------------
    24. // 动态成员
    25. interface Cache {
    26. [prop: string]: string
    27. }
    28. const cache: Cache = {
    29. 123: "123"
    30. }