Typescript: constants in an interface

后端 未结 5 1303
一生所求
一生所求 2020-12-17 14:31

How do I place a constant in an Interface in typescript. Like in java it is:

interface OlympicMedal {
  static final String GOLD = \"Gold\";
  static final S         


        
5条回答
  •  温柔的废话
    2020-12-17 15:05

    Just use the value in the interface in place of the type, see below

    export interface TypeX {
        "pk": "fixed"
    }
    
    let x1 : TypeX = {
        "pk":"fixed" // this is ok
    }
    
    let x2 : TypeX = {
        "pk":"something else" // error TS2322: Type '"something else"' is not assignable to type '"fixed"'.
    }
    

提交回复
热议问题