zhenghao li
zhenghao li

Reputation: 57

typescript what does square brackets mean in interface?

I see the following code in vue github link

declare const RefSymbol: unique symbol
export declare const RawSymbol: unique symbol

export interface Ref<T = any> {
  value: T
  
  [RefSymbol]: true
}

what is RefSymbol mean here? i have tried this code:

let test:Ref={value:1}

vscode tell me that I am missing attributes of [RefSymbol]

i had read these questions:

  1. stackoverflow link
  2. https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types

some example code is

interface StringArray {
  [index: number]: string;
}
 
let myArray: StringArray;
myArray = ["Bob", "Fred"];
 
let myStr: string = myArray[0];

which is quite different with the first example code , it use:true rather than type :string , and i did not know how to pass value to [RefSymbol] ,can anyone explain this thing? thanks for any reply

Upvotes: 1

Views: 585

Answers (1)

Tachibana Shin
Tachibana Shin

Reputation: 3898

oh it's a javascript declaration where RefSymbol is a constant. It's like this.

const key = "size"

const map = {
  [key]: 0
}

console.log(map[key]) // 0

typesccript declare also RefSymbol is declared as unique symbol which means it is a unique and invariant symbol set as key

Upvotes: 2

Related Questions