简体   繁体   English

typescript:您将如何返回具有相同 arguments 但不同类型的新 function 的 ZDBC11CAA5BDA929F77E6FB4DBEDA88

[英]typescript: How would you return a new function with the same arguments but different types for those arguments?

const stringToDate = ( x: string ) => new Date( x )
//    stringToDate: (x: string) => Date

type MapTupleValues<Tuple extends any[], NewValue> = {
    [ index in keyof Tuple ]: NewValue
}

type TestMappedTuple = MapTupleValues<[ a: Date, b: Date ], string>
//   TestMappedTuple = [a: string, b: string]
// works fine

function modifyFn<Fn extends ( ...args: Date[] ) => any> ( fn: Fn ) {
    return ( ...args: MapTupleValues<Parameters<Fn>, string> ): ReturnType<Fn> => {
        //   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error
        //   A rest parameter must be of an array type.

        return fn( ...args.map( x => stringToDate( x ) ) )
    }
}

const daysDifference = ( start: Date, end: Date ) => ( end.getTime() - start.getTime() ) / ( 1000 * 60 * 60 * 24 )
//    daysDifference: (start: Date, end: Date) => number

const modifiedDaysDifference = modifyFn( daysDifference )
//    modifiedDaysDifference: (start: string, end: string) => number

console.log( daysDifference( new Date( '2022-07-29' ), new Date( '2022-08-21' ) ) ) // 23
console.log( modifiedDaysDifference( '2022-07-29', '2022-08-21' ) ) // 23

this seems to be working, even though there is an error in modifyFn这似乎有效,即使modifyFn有错误
how can I get rid of the error while still getting the same result?如何在获得相同结果的同时摆脱错误?

Thanks jcalz谢谢 jcalz

Here's their answer:这是他们的答案:

const stringToDate = (x: string) => new Date(x)
//    stringToDate: (x: string) => Date

type MapTupleValues<T extends any[], V> = Extract<{
    [I in keyof T]: V
}, any[]>

type TestMappedTuple = MapTupleValues<[a: Date, b: Date], string>
//   TestMappedTuple = [a: string, b: string]
// works fine

function modifyFn<Fn extends (...args: Date[]) => any>(fn: Fn) {
    return (...args: MapTupleValues<Parameters<Fn>, string>): ReturnType<Fn> => {
        return fn(...args.map(x => stringToDate(x)))
    }
}

const daysDifference = (start: Date, end: Date) => (end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24)
//    daysDifference: (start: Date, end: Date) => number

const modifiedDaysDifference = modifyFn(daysDifference)
//    modifiedDaysDifference: (start: string, end: string) => number

console.log(daysDifference(new Date('2022-07-29'), new Date('2022-08-21'))) // 23
console.log(modifiedDaysDifference('2022-07-29', '2022-08-21')) // 23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript 相同 function arguments 类型不同的实现 - TypeScript same function arguments type with different implementation 如何在 Typescript 中映射函数的两个参数的类型? - How to map types of two arguments of function in Typescript? TypeScript:如何在破坏参数时指定函数参数的类型? - TypeScript: How to specify types for function arguments when destructing arguments? Typescript,不同 function arguments - Typescript, different function arguments Typescript 是否可以自动检测 function arguments 的类型,然后在 ZC1C425268E48385D1AB507 中使用这些类型? - Can Typescript automatically detect the types of function arguments and then use those types in the function? TypeScript 中被破坏的函数参数的类型? - Types for destructed function arguments in TypeScript? TypeScript:是否可以定义一个接受不同类型 arguments 的可变参数 function? - TypeScript: Is it possible to define a variadic function that accepts different types of arguments? 在 typescript 中,如果与 arguments 同名,如何访问 function 之外的变量 - In typescript, how do you access variables outside of the function if they have the same name as arguments 根据参数打字稿不同的类属性类型 - Typescript different class property types depending on arguments Typescript function arguments 基于以枚举为键的类型 - Typescript function arguments based on types with enums as keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM