TypeScript(JavaScript)中奇怪的类属性赋值:'?='
我刚刚在类属性声明中看到 ?= 赋值表达式。
有人能给我解释一下这是什么意思吗?
我知道新的可选链(object?.prop),但这个对我来说是未知的。所以我不确定这是一个错误还是一个真正的语言特性?
export class GoogleConfig {
GOOGLE_CLIENT_ID: string
GOOGLE_CLIENT_SECRET: string
GOOGLE_CALLBACK_URL?='http://localhost/frontend'
GOOGLE_CALLBACK_URL_INTERNAL?='http://localhost/auth/google'
}
谢谢任何帮助!
答案#1
默认情况下,在 interface 中声明的普通属性是必需的,但您可以使用 ?: 声明可选属性。
class ShapeConfig {
type: string;
color?: string;
width?: number;
}
这里,type 是必需的,其余是可选的。
答案#2
这似乎是一个可选的类属性,且省略了类型定义
export class GoogleConfig {
GOOGLE_CLIENT_ID: string
GOOGLE_CLIENT_SECRET: string
GOOGLE_CALLBACK_URL?:string ='http://localhost/frontend'
GOOGLE_CALLBACK_URL_INTERNAL?:string ='http://localhost/auth/google'
}
