属性 'observers' 的类型不兼容。为什么?
Types of property 'observers' are incompatible. Why?
我正在尝试创建一个仅接受 AvailableLanguages 枚举值的 BehaviorSubject,但编译器出于某种原因不同意我的代码。
你能告诉我为什么吗?
我正在使用 Angular 12.2.10.
这是服务:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
export enum AvailableLanguages {
EN = "en-US",
IT = "it-IT"
}
@Injectable({
providedIn: 'root'
})
export class UserService {
_selectedLanguage$: BehaviorSubject<AvailableLanguages> = new BehaviorSubject(AvailableLanguages.EN);
constructor() { }
}
这是编译器的错误:
Error: src/app/services/user.service.ts:15:3 - error TS2322: Type 'BehaviorSubject<AvailableLanguages.EN>' is not assignable to type 'BehaviorSubject<AvailableLanguages>'.
Types of property 'observers' are incompatible.
Type 'Observer<AvailableLanguages.EN>[]' is not assignable to type 'Observer<AvailableLanguages>[]'.
Type 'Observer<AvailableLanguages.EN>' is not assignable to type 'Observer<AvailableLanguages>'.
Type 'AvailableLanguages' is not assignable to type 'AvailableLanguages.EN'.
15 _selectedLanguage$: BehaviorSubject<AvailableLanguages> = new BehaviorSubject(AvailableLanguages.EN);
~~~~~~~~~~~~~~~~~~
× Failed to compile.
嗯,可能是因为您在 tsconfig.json
中使用了 strict
?
不过,这本身不是问题。你只需要确保你的类型是“对齐的”。
所以,你的声明:
_selectedLanguage$: BehaviorSubject<AvailableLanguages> = new BehaviorSubject(AvailableLanguages.EN);
应该是:
_selectedLanguage$: BehaviorSubject<AvailableLanguages> = new BehaviorSubject<AvailableLanguages>(AvailableLanguages.EN);
或者简单地让 TSC 推断正确的类型:
_selectedLanguage$ = new BehaviorSubject<AvailableLanguages>(AvailableLanguages.EN);
我正在尝试创建一个仅接受 AvailableLanguages 枚举值的 BehaviorSubject,但编译器出于某种原因不同意我的代码。 你能告诉我为什么吗?
我正在使用 Angular 12.2.10.
这是服务:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
export enum AvailableLanguages {
EN = "en-US",
IT = "it-IT"
}
@Injectable({
providedIn: 'root'
})
export class UserService {
_selectedLanguage$: BehaviorSubject<AvailableLanguages> = new BehaviorSubject(AvailableLanguages.EN);
constructor() { }
}
这是编译器的错误:
Error: src/app/services/user.service.ts:15:3 - error TS2322: Type 'BehaviorSubject<AvailableLanguages.EN>' is not assignable to type 'BehaviorSubject<AvailableLanguages>'.
Types of property 'observers' are incompatible.
Type 'Observer<AvailableLanguages.EN>[]' is not assignable to type 'Observer<AvailableLanguages>[]'.
Type 'Observer<AvailableLanguages.EN>' is not assignable to type 'Observer<AvailableLanguages>'.
Type 'AvailableLanguages' is not assignable to type 'AvailableLanguages.EN'.
15 _selectedLanguage$: BehaviorSubject<AvailableLanguages> = new BehaviorSubject(AvailableLanguages.EN);
~~~~~~~~~~~~~~~~~~
× Failed to compile.
嗯,可能是因为您在 tsconfig.json
中使用了 strict
?
不过,这本身不是问题。你只需要确保你的类型是“对齐的”。
所以,你的声明:
_selectedLanguage$: BehaviorSubject<AvailableLanguages> = new BehaviorSubject(AvailableLanguages.EN);
应该是:
_selectedLanguage$: BehaviorSubject<AvailableLanguages> = new BehaviorSubject<AvailableLanguages>(AvailableLanguages.EN);
或者简单地让 TSC 推断正确的类型:
_selectedLanguage$ = new BehaviorSubject<AvailableLanguages>(AvailableLanguages.EN);