This topic is designed to give you a better understanding of the TS type system (type programming), write your own type tools, or simply enjoy the challenge!
Implement a Pick
Implement the built-in Pick<T, K> for TS, but do not use it.
Select attribute K from type T to construct a new type.
Such as:
interface Todo {
title: string
description: string
completed: boolean
}
type TodoPreview = MyPick<Todo, 'title' | 'completed'>
const todo: TodoPreview = {
title: 'Clean room',
completed: false,
}
Copy the code
The answer:
Realize the Readonly
Don’t use the built-in Readonly, implement one yourself.
This Readonly takes a generic parameter and returns exactly the same type, except that all properties are embellished by Readonly.
That is, you can no longer assign values to attributes of the object.
Such as:
interface Todo {
title: string
description: string
}
const todo: MyReadonly<Todo> = {
title: "Hey",
description: "foobar"
}
todo.title = "Hello" // Error: cannot reassign a readonly property
todo.description = "barFoo" // Error: cannot reassign a readonly property
Copy the code
The answer:
Tuples are converted to objects
Pass in a tuple type and convert the tuple type to an object type whose keys/values are iterated from the tuple.
Such as:
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
const result: TupleToObject<typeof tuple> // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
Copy the code
The answer:
The first element
Implement a generic First that takes an array T and returns the type of its First element.
Such as:
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
type head1 = First<arr1> // expected to be 'a'
type head2 = First<arr2> // expected to be 3
Copy the code
The answer:
Gets the length of the element
Create a generic Length that takes a readonly array and returns the Length of the array.
Such as:
type tesla = ['tesla', 'model 3', 'model X', 'model Y']
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']
type teslaLength = Length<tesla> // expected 4
type spaceXLength = Length<spaceX> // expected 5
Copy the code
The answer:
To realize the Exclude
Implement the built-in Exclude <T, U>
Exclude from T those types that can be assigned to U
The answer:
Awaited
If we have a Promise object, the Promise object will return a type. In TS, we use the T in the Promise to describe the type returned by this Promise. You implement a type that can be retrieved.
For example: Promise, return type ExampleType.
The answer:
If
Implement an If util that takes a C generic argument and returns type T If true and type F If false
Such as:
type A = If<true, 'a', 'b'> // expected to be 'a'
type B = If<false, 'a', 'b'> // expected to be 'b'
Copy the code
Concat
Implements a Concat version of the array type
Such as:
type Result = Concat<[1], [2]> // expected to be [1, 2]
Copy the code
The answer:
Includes
❝
Implement the JavaScript array.includes function in the type system. A type takes two arguments. The output should be a Boolean value of true or false.
❞
Such as:
type isPillarMen = Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'> // expected to be `false`
Copy the code
This problem is difficult, anyway I am copy!!
Push
Implement the array. push version
Such as:
type Result = Push<[1, 2], '3'> // [1, 2, '3']
Copy the code
The answer:
Unshift
Implement the type version of array. unshift
Such as:
type Result = Unshift<[1, 2], 0> // [0, 1, 2,]
Copy the code
The answer:
The last
I’ve done all of these before, so please leave a comment in the comments section
reference
- Github.com/type-challe…
- www.typescriptlang.org/docs/handbo…