跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • Typescript 里的这种写法是什么意思?
未分類
17 1 月 2021

Typescript 里的这种写法是什么意思?

Typescript 里的这种写法是什么意思?

資深大佬 : tsuih 7

在看源码的时候发现下面这种写法,不明白是什么意思,请教一下各位:

const temp: ToughCookieFileStore = {     [domain]: {         [path]: {             [name]: {                 key: name,                 value: etcCookie.value,                 expires,                 domain,                 path,                 secure: etcCookie.secure,                 httpOnly: etcCookie.httpOnly,                 hostOnly: etcCookie.hostOnly,             },         },     }, }; 

下面是接口的声明

 export interface ToughCookieFileStore {   [site: string]: TCFSPaths; }  export interface TCFSPaths {   [site: string]: TCFSCookies; }  export interface TCFSCookies {   [cookieName: string]: TCFSCookieAttributes; }  export interface TCFSCookieAttributes {   key: string;   value: string;   expires?: string;   maxAge?: number;   domain: string;   path: string;   secure?: boolean;   httpOnly?: boolean;   extensions?: string[];   hostOnly: boolean;   creation?: string;   lastAccessed?: string; } 

大佬有話說 (5)

  • 資深大佬 : CismonX

    https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types

  • 資深大佬 : noe132

    domain 是变量,用 domain 的值做 key 。方括号内可以是任意表达式

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

    不要被 ts 骗了,这是 js 的语法

  • 資深大佬 : alexkuang

    方括号里是对象的动态键名,es6 语法跟 ts 无关

  • 資深大佬 : bearboss

    索引签名,外面两层实际上并没有什么限制,可以设置任意的字符串作为属性,最后这层才需要符合 TCFSCookieAttributes 的定义

  • 主 資深大佬 : tsuih

    @noe132 多谢,很有帮助

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具