given
function fn(a:number, b:number, c:number): void {} type T1 = Parameters<typeof fn> // [a:number, b:number, c:number]
I would like to
type T2<T1> = /* how?! --> [number, number, number] */
Why?
I would like to assign an "untyped" value to a variable of type T1
, but it doesn't work:
type T1 = Parameters<typeof fn> // [a:number, b:number, c:number] let foo = [1, 2, 3] // inferred type --> number[] let bar: T1 = foo // Error: Target requires 3 element(s) but source may have fewer.ts(2322)
It works like this (typed foo):
let foo: [number, number, number] = [1, 2, 3] let bar: T1 = foo // ok!
To get around this, I would like to create a new type T2
based on T1
which would "mirror" the parameters of fn
as tuple array (which accepts the inferred type number[]
). This way I would have type safety (length and type of fn
parameters) but could pass "untyped" values to a T2
typed variable:
type T2<T1> = /* how?! --> [number, number, number] */ let foo = [1, 2, 3] // inferred type --> number[] let bar: T2 = foo // ok! let bar: [number, number, number] = foo // ok!
Thanks!
https://stackoverflow.com/questions/65755553/typescript-convert-parameters-array-to-tuple-array January 17, 2021 at 06:51AM
没有评论:
发表评论