[在使用rest参数将参数转发给curried函数时如何处理可选的tuple元素(使用strictNullChecking)

问题描述 投票:0回答:1

目标:摆脱打字稿错误TS2345,其中参数不可分配给参数

问题:

我试图通过使用rest参数和tuple作为类型转发所有参数来咖喱箭功能。但是,由于我使用了一个可选元素,而strictNullChecking则会引发错误,因为它传递了未定义的类型作为可能,而如果未给出参数,则它应该(并且不会使用)该参数。

代码(关于下层函数的解析,格式化,转换):

/* eslint-disable @typescript-eslint/ban-ts-ignore */ import { parse, format } from 'date-fns' import curry from 'lodash/curry' type Formats = { [key: string]: string } type Parse = [(keyof Formats), string?] type Format = [keyof Formats, (Date | number)?] type Convert = [keyof Formats, keyof Formats, string?] // if you need prototype inheritance then convert functions to methods and bind export class DateFormatter<T extends Formats> { formats: T constructor (formats: T) { this.formats = formats } parseToDate = (formatType: keyof T, date: string): Date => { return parse(date, this.formats[formatType], new Date()) } formatToString = (formatType: keyof T, date: Date | number) => { return format(date, this.formats[formatType]) } convertFormat = (from: keyof T, to: keyof T, formattedDate: string): string => { return format(parse(formattedDate, this.formats[from], new Date()), this.formats[to]) } // @ts-ignore parse = (...args: Parse) => curry(this.parseToDate).apply(this, args) // @ts-ignore format = (...args: Format) => curry(this.formatToString).apply(this, args) // @ts-ignore convert = (...args: Convert) => curry(this.convertFormat).apply(this, args) }

沙盒

https://codesandbox.io/s/summer-fog-sw7qb?fontsize=14&hidenavigation=1&theme=dark任何提示和帮助,不胜感激!
typescript
1个回答
1
投票
import { parse, format } from "date-fns"; import _ from "lodash"; type Formats = { [key: string]: string }; type Parse = [(keyof Formats), string?]; type Format = [keyof Formats, (Date | number)?]; type Convert = [keyof Formats, keyof Formats, string?]; // if you need prototype inheritance then convert functions to methods and bind export class DateFormatter<T extends Formats> { formats: T; constructor(formats: T) { this.formats = formats; } parseToDate = (formatType: keyof T, date: string): Date => { return parse(date, this.formats[formatType], new Date()); }; formatToString = (formatType: keyof T, date: Date | number) => { return format(date, this.formats[formatType]); }; convertFormat = ( from: keyof T, to: keyof T, formattedDate: string ): string => { return format( parse(formattedDate, this.formats[from], new Date()), this.formats[to] ); }; parse: { (formatType: keyof T, date: string): Date; (formatType: keyof T): (date: string) => Date } = (...args: any) => _.curry(this.parseToDate).apply(this, args) as any; format: { (formatType: keyof T, date: Date | number): string; (formatType: keyof T):(date: Date | number) => string; } = (...args: any) => _.curry(this.formatToString).apply(this, args) as any; convert: { (from: keyof T, to: keyof T, formattedDate: string): string; (from: keyof T, to: keyof T): (formattedDate: string) => string; } = (...args: any) => _.curry(this.convertFormat).apply(this, args) as any; }

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.