【发布时间】:2021-11-10 05:56:52
【问题描述】:
我正在使用对象内部的BehaviorSubjects 以角度构建服务文件,并在 VS Code 中收到以下错误。
'string' 类型的参数不能分配给'never' 类型的参数。
我之前以同样的方式使用过BehaviorSubjects,所以我无法弄清楚在这种情况下我做错了什么。我在服务文件中有很多事情要做,所以我将把它省略为我正在尝试设计的模式类型的单个实例。
//a set of values a user will select through an input.
export type MtSizeOptionType = '--'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'10'|'11'|
'12'|'13'|'14'|'15'|'16'|'17'|'18'|'19'|'20'|'21'|'22'|
'23'|'24'|'25'|'26'|'27'|'28'|'29'|'30'|'31'|'32'|'33'|
'34'|'35'|'36'|'37'|'38'|'39'|'40'|'41'|'42'|'43'|'44';
//a type for shaping one of the types of datasets I want to use.
export type MtSizeObserver = {
params : BehaviorSubject<MtSizeOptionType>,
setting : BehaviorSubject<string>
};
//a type for allowing the data object to be shaped as one of these data types which I'm
//leaving out for brevity.
export type PropObserver = NumberObserver | StringObserver | BoxSizeObserver |
MtSizeObserver | FontSizeObserver | LineSizeObserver;
//an interface to shape the data entries.
export interface MainObserver{ [key : string] : PropObserver; }
//a function I import into the service file to create a string to store as a setting.
export function CreateMtSize(size: string): string { return `mt-${size}`; }
@Injectable({
providedIn: 'root'
})
export class ResponsiveShellService {
//the data object
private PropertyMaster : MainObserver = {
CrushGapWSetting: {
params : new BehaviorSubject<MtSizeOptionType>('--'),
setting : new BehaviorSubject<string>('mt-1')
},
//all the other elements
}
constructor(){}
//the function where I attempt to update the CrushGapWSetting
public updateCrushGapW(val: MtSizeOptionType){
const defaultSize: string = '--mt-1';
this.PropertyMaster.CrushGapWSetting.params.next(val); //error
if(val === '--'){
this.PropertyMaster.CrushGapWSetting.setting.next(defaultSize); //error
}
else {
this.PropertyMaster.CrushGapWSetting.setting.next(CreateMtSize(val)); //error
}
}
}
updateCrushGapW() 函数中的每个 .next() 都会抛出 not assignable to parameter type of 'never' 错误,我不明白它是如何输入为 never 的。谁能看到这是怎么回事?如果您需要查看其他类型,请告诉我,我会添加它们。
【问题讨论】:
-
为 BehaviourSubject 提供初始值。 BehaviourSubject 需要初始值。你可以在这里查看 - learnrxjs.io/learn-rxjs/subjects/behaviorsubject
标签: angular typescript rxjs