升级到 Angular 11 后扩展的 DecimalPipe 出现问题
Problem with extended DecimalPipe after upgrading to Angular 11
从 Angular 10 升级到 11 和 运行 我们在 DecimalPipe 上的扩展出现问题。
管道代码:
export class CustomNumberFormatPipe extends DecimalPipe implements PipeTransform {
transform(value: string): string | null {
if (!value) {
return null;
}
value = value.toString().replace(/,/g, '');
return super.transform(value);
}
和错误:
Property 'transform' in type 'CustomNumberFormatPipe' is not assignable to the same property in base type 'DecimalPipe'.
Type '(value: string) => string' is not assignable to type '{ (value: string | number,
digitsInfo?: string, locale?: string): string; (value: null, digitsInfo?: string, locale?:
string): null; (value: string | number, digitsInfo?: string, locale?: string):
string; }'.
Type 'string' is not assignable to type 'null'.
和 DecimalType 定义:
export declare class DecimalPipe implements PipeTransform {
private _locale;
constructor(_locale: string);
transform(value: number | string, digitsInfo?: string, locale?: string): string | null;
transform(value: null | undefined, digitsInfo?: string, locale?: string): null;
transform(value: number | string | null | undefined, digitsInfo?: string, locale?: string): string | null;
static ɵfac: ɵngcc0.ɵɵFactoryDef<DecimalPipe, never>;
static ɵpipe: ɵngcc0.ɵɵPipeDefWithMeta<DecimalPipe, "number">;
}
到目前为止,我找到的唯一解决方案是共同修改实际的 DecimalPipe,但我真的没有信心这样做。提前致谢。
只需转到 DecimalPipe
声明并将其 transform
签名复制到您的管道:
transform(value: number | string, digitsInfo?: string, locale?: string): string | null;
transform(value: null | undefined, digitsInfo?: string, locale?: string): null;
transform(value: number | string | null | undefined, digitsInfo?: string, locale?: string): string | null {
if (!value) {
return null;
}
value = value.toString().replace(/,/g, '');
return super.transform(value);
}
从 Angular 10 升级到 11 和 运行 我们在 DecimalPipe 上的扩展出现问题。
管道代码:
export class CustomNumberFormatPipe extends DecimalPipe implements PipeTransform {
transform(value: string): string | null {
if (!value) {
return null;
}
value = value.toString().replace(/,/g, '');
return super.transform(value);
}
和错误:
Property 'transform' in type 'CustomNumberFormatPipe' is not assignable to the same property in base type 'DecimalPipe'.
Type '(value: string) => string' is not assignable to type '{ (value: string | number,
digitsInfo?: string, locale?: string): string; (value: null, digitsInfo?: string, locale?:
string): null; (value: string | number, digitsInfo?: string, locale?: string):
string; }'.
Type 'string' is not assignable to type 'null'.
和 DecimalType 定义:
export declare class DecimalPipe implements PipeTransform {
private _locale;
constructor(_locale: string);
transform(value: number | string, digitsInfo?: string, locale?: string): string | null;
transform(value: null | undefined, digitsInfo?: string, locale?: string): null;
transform(value: number | string | null | undefined, digitsInfo?: string, locale?: string): string | null;
static ɵfac: ɵngcc0.ɵɵFactoryDef<DecimalPipe, never>;
static ɵpipe: ɵngcc0.ɵɵPipeDefWithMeta<DecimalPipe, "number">;
}
到目前为止,我找到的唯一解决方案是共同修改实际的 DecimalPipe,但我真的没有信心这样做。提前致谢。
只需转到 DecimalPipe
声明并将其 transform
签名复制到您的管道:
transform(value: number | string, digitsInfo?: string, locale?: string): string | null;
transform(value: null | undefined, digitsInfo?: string, locale?: string): null;
transform(value: number | string | null | undefined, digitsInfo?: string, locale?: string): string | null {
if (!value) {
return null;
}
value = value.toString().replace(/,/g, '');
return super.transform(value);
}