需求:创建一个通用的Length,接受一个readonly的数组,返回这个数组的长度。
type Length<T extends readonly any[]> = any
import type { Equal, Expect } from '@type-challenges/utils'
const tesla = ['tesla', 'model 3', 'model X', 'model Y'] as const
const spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'] as const
type cases = [
Expect<Equal<Length<typeof tesla>, 4>>,
Expect<Equal<Length<typeof spaceX>, 5>>,
// @ts-expect-error
Length<5>,
// @ts-expect-error
Length<'hello world'>,
]
先用js的思路写一遍:
- 判断是否是数组
- 返回数组长度
function lengthOfNum(arr){
if(!Array.isArray(arr))
return;
return arr.length
}