阅读提示:
每个方法都有相应的描述、语法、参数、返回值、注意项(可选)、例子(可选)。
语法中的[]
里面中的内容表示参数为可选参数。
Array.from()
描述:从一个类似数组或可迭代对象中创建一个新的数组实例。
语法:
new_array = Array.from(arrayLike[, callback(element[, index[, array]])[, thisArg]]);
樊小书生的博客,多看代码,多看书,付出总会有收获的。
阅读提示:
每个方法都有相应的描述、语法、参数、返回值、注意项(可选)、例子(可选)。
语法中的[]
里面中的内容表示参数为可选参数。
Array.from()
描述:从一个类似数组或可迭代对象中创建一个新的数组实例。
语法:
new_array = Array.from(arrayLike[, callback(element[, index[, array]])[, thisArg]]);
by jiangshan (@jiangshanmeta) #中等 #tuple
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
by キリサメ qianxi (@qianxi0410) #中等 #tuple
Do you know lodash
? Chunk
is a very useful function in it, now let's implement it.
Chunk<T, N>
accepts two required type parameters, the T
must be a tuple
, and the N
must be an integer >=1
by キリサメ qianxi (@qianxi0410) #中等 #tuple
Fill
, a common JavaScript function, now let us implement it with types.
Fill<T, N, Start?, End?>
, as you can see,Fill
accepts four types of parameters, of which T
and N
are required parameters, and Start
and End
are optional parameters.
The requirements for these parameters are: T
must be a tuple
, N
can be any type of value, Start
and End
must be integers greater than or equal to 0.
by Yugang Cao (@Talljack) #中等 #template-literal
实现 TrimRight<T>
,它接收确定的字符串类型并返回一个新的字符串,其中新返回的字符串删除了原字符串结尾的空白字符串。
例如
type Trimed = TrimRight<' Hello World '> // 应推导出 ' Hello World'
by jiangshan (@jiangshanmeta) #中等 #template-literal
Implement the type version of Math.trunc
, which takes string or number and returns the integer part of a number by removing any fractional digits.
For example:
type A = Trunc<12.34> // 12
by ch3cknull (@ch3cknull) #中等 #array
In This Challenge, You should implement a type GreaterThan<T, U>
like T > U
Negative numbers do not need to be considered.
For example
GreaterThan<2, 1> //should be true
GreaterThan<1, 1> //should be false
GreaterThan<10, 100> //should be false
GreaterThan<111, 11> //should be true
by キリサメ qianxi (@qianxi0410) #中等 #tuple
In This Challenge, You should implement a type Zip<T, U>
, T and U must be Tuple
type exp = Zip<[1, 2], [true, false]> // expected to be [[1, true], [2, false]]
by jiangshan (@jiangshanmeta) #中等 #array
Implement the type version of Array.shift
For example
type Result = Shift<[3, 2, 1]> // [2, 1]
by jiangshan (@jiangshanmeta) #中等 #object #tuple
Given a tuple type T
that only contains string type, and a type U
, build an object recursively.
type a = TupleToNestedObject<['a'], string> // {a: string}
type b = TupleToNestedObject<['a', 'b'], number> // {a: {b: number}}
type c = TupleToNestedObject<[], boolean> // boolean. if the tuple is empty, just return the U type