【问题标题】:Angular: how to define two possible values for a string variableAngular:如何为字符串变量定义两个可能的值
【发布时间】:2022-07-05 23:32:28
【问题描述】:

是否可以定义一个字符串类型的变量,然后定义该字符串可以有哪些可能的值?

colorScheme?: string = 'positive|negative'; //must be a string 
                                             and must have the value "postiive" or "negative"

谢谢。

【问题讨论】:

  • colorScheme?: 'positive' | 'negative = ...'

标签: angular typescript assign


【解决方案1】:

是的,它叫做union type

colorScheme?: 'positive'|'negative'= 'negative';

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多