首页 > 其他分享 >[Typescript] 66. Medium - IsTuple

[Typescript] 66. Medium - IsTuple

时间:2022-10-23 20:58:08浏览次数:43  
标签:Typescript false _____________ type IsTuple Medium Expect 66 true

Implement a type IsTuple, which takes an input type T and returns whether T is tuple type.

For example:

type case1 = IsTuple<[number]> // true
type case2 = IsTuple<readonly [number]> // true
type case3 = IsTuple<number[]> // false

 

/* _____________ Your Code Here _____________ */

type IsTuple<T> = [T] extends [never] 
  ? false
  : T extends readonly any[] 
    ? any[] extends T 
      ? false
      : true
    : false;
type x = never extends any[] ? true: false
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<IsTuple<[]>, true>>,
  Expect<Equal<IsTuple<[number]>, true>>,
  Expect<Equal<IsTuple<readonly [1]>, true>>,
  Expect<Equal<IsTuple<{ length: 1 }>, false>>,
  Expect<Equal<IsTuple<number[]>, false>>,
  Expect<Equal<IsTuple<never>, false>>,
]

 

标签:Typescript,false,_____________,type,IsTuple,Medium,Expect,66,true
From: https://www.cnblogs.com/Answer1215/p/16819487.html

相关文章

  • [Typescript] 65. Medium - Zip
    InThisChallenge,Youshouldimplementatype Zip<T,U>,TandUmustbe Tupletypeexp=Zip<[1,2],[true,false]>//expectedtobe[[1,true],[2,false......
  • TypeScript
    一、环境准备ts和js的区别ts属于静态类型,写代码时就能检查错误。是js的超类,包含js功能,多的是类型。js属于动态类型,只有在运行时才会报错,不会检查类型是否发生变化。......
  • [Typescript] 64. Hard - AllCombinations
    Implementtype AllCombinations<S> thatreturnallcombinationsofstringswhichusecharactersfrom S atmostonce.Forexample:typeAllCombinations_ABC=......
  • [Typescript] 63. Medium - Greater Than
    InThisChallenge,Youshouldimplementatype GreaterThan<T,U> like T>UNegativenumbersdonotneedtobeconsidered.ForexampleGreaterThan<2,1>//s......
  • TypeScript 复习进阶三部曲 (1) – 把 TypeScript 当强类型语言使用
    前言本来是想照着TypeScript官网handbook写教程的.但真的没那个mood.还是用我自己的方式写教程(其实是复习和进阶笔记)吧 学习TypeScript的三个阶段(三部......
  • 【LeetCode 904-medium】水果成篮
    1、 这题做法多种多样,看能不能找到五种做法。 其实就是这道题:​ 力扣题我的AC代码:(法1:边遍历边维护答案,如果遇到f[i]是第三个数就抛弃除了f[i-1]的另一个数)classSolutio......
  • 云安全攻防体系实践-CVE-2018-15664分析
    CVE-2018-15664符号链接替换漏洞如果在docker守护进程检查复制路径时,攻击者可以利用中间的间隙,先在这里放置一个非符号链接的常规文件或目录,检查结束后,攻击者赶在Docker守护......
  • TypeScript infer All In One
    TypeScriptinferAllInOnehttps://www.typescriptlang.org/docs/handbook/type-inference.htmlhttps://www.cnblogs.com/xgqfrms/tag/infer/demos//Herewedeclar......
  • ABC266 ~ 273 题解
    缺省源#pragmaGCCoptimize(3)#include<bits/stdc++.h>namespace//tofoldthatjunkcode{#definefilein(x){freopen(x".in","r",stdin);}#definefile(......
  • [Typescript] Tips: Use assertion functions inside classes
    Youcandosomereally,reallyneatstuffwithassertionfunctionsinsideclasses.Here,weassertthattheuserisloggedinandgetproperinferenceontheu......