TypeScript中元组的最后一个元素的类型提取方式

huangapple go评论54阅读模式
英文:

Extract type of last element of tuple in TypeScript

问题

You can achieve the desired type LastElementOf<T> using conditional types in TypeScript. Here's the code to define such a type:

type LastElementOf<T extends any[]> = T extends [...infer Rest, infer Last] ? Last : never;

This type definition will correctly extract the type of the last element in the array or tuple T.

英文:

Essentially, what I am trying to write is a function that accepts an array of different types, (do something with that), and then return a type based on only the last element in that array (previous elements have been processed, but only the last element is used for the return value).

I know I can write a function that returns an array based off of all element types as follows.

function arraymod&lt;T extends unknown[]&gt;(
  ...input: {
    [index in keyof T]: ParamType&lt;T[index]&gt;
  }
): T {
  return input.map(unparam);
}

What I want is a function that does something like this.

function takelast&lt;T extends unknown[]&gt;(
  ...input: {
    [index in keyof T]: ParamType&lt;T[index]&gt;
  }
): LastElementOf&lt;T&gt; /* What would this return type be? */ {
  return unparam(input[input.length - 1]);

(ParamType&lt;T&gt; and unparam are just some example types/functions for illustration)

How can I write a type LastElementOf&lt;T&gt; that gives the type of the last element of a tuple T (or array, eventhough that would just be type of any element).

答案1

得分: 3

We must use infer keyword to achieve this result.

We are going to check the array to extend infer last item and the rest of them, which we don't care about:

type LastElementOf<T extends readonly unknown[]> = T extends readonly [...unknown[], infer Last]
  ? Last
  : never;

...unknown[] will be first items except the last one, and the last one will be the infer Last

Usage:

type Case1 = LastElementOf<[]>; // never
type Case2 = LastElementOf<[&#39;dasd&#39;]>; // &quot;dasd&#39;
type Case3 = LastElementOf<[&#39;string&#39;, 12, false]>; // false
type Case4 = LastElementOf<readonly [&#39;string&#39;, 12, false]>; // false

playground

英文:

We must use infer keyword to achieve this result.

We are going to check the array to extend infer last item and the rest of them, which we don't care about:

type LastElementOf&lt;T extends readonly unknown[]&gt; = T extends readonly [...unknown[], infer Last]
  ? Last
  : never;

...unknown[] will be first items except the last one, and the last one will be the infer Last

Usage:

type Case1 = LastElementOf&lt;[]&gt;; // never
type Case2 = LastElementOf&lt;[&#39;dasd&#39;]&gt;; // &quot;dasd&#39;
type Case3 = LastElementOf&lt;[&#39;string&#39;, 12, false]&gt;; // false
type Case4 = LastElementOf&lt;readonly [&#39;string&#39;, 12, false]&gt;; // false

playground

huangapple
  • 本文由 发表于 2023年5月18日 06:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定