Current location - Quotes Website - Signature design - TypeScript Type Challenge Medium
TypeScript Type Challenge Medium

[[toc]]

Medium , #infer , #built-in

Implements TS's built-in ReturnType , but cannot use it.

Answer

Medium , #union , #built-in

Implement TypeScript's Omit generics without using Omit.

Omit creates a T object with the fields in K omitted.

Answer

Medium , #readonly , #object-keys

Implement a general MyReadonly2 with two types of parameters T and K.

K specifies the set of properties of T that should be set to Readonly. If K is not provided, all properties should be made read-only, just like a normal Readonly.

Answer

Medium , #readonly , #object-keys , #deep

Implement a generic DeepReadonly that reads each parameter of the object and its sub-objects are made read-only recursively.

You can assume that in this challenge we are only dealing with objects. Arrays, functions, classes, etc. don't need to be considered. However, you can still challenge yourself by covering as many different cases as possible.

Answer

Medium , #infer , #tuple , #union

Implements the generic TupleToUnion , which returns the set of all values ??of a tuple.

Answer

Medium , #application

In JavaScript we often use chainable/Pipeline functions to construct an object, but in TypeScript , can you reasonably attach a type to him?

In this challenge, you can implement the type any way you like - Interface, Type or Class. You need to provide two functions option(key, value) and get() . In option you need to extend the current object type with the provided key and value, and get the final result through get.

You only need to implement this functionality at the type level - there is no need to implement any actual TS/JS logic.

You can assume that key only accepts strings and value accepts any type, you just need to expose the type it is passed without any processing. The same key will only be used once.

Answer

Medium , #array

Implement a generic Last that accepts an array T and returns the type of its last element.

Answer

Medium , #array

Implement a generic Pop that accepts an array T and returns an array without the last element.

Extra: Likewise, can you implement Shift , Push and Unshift as well?

Answer

Medium , #array , #built-in

Type the function PromiseAll, which accepts an array of PromiseLike objects, and the return value should be Promise, Where T is the parsed result array.

Answer

Medium , #union , `#map

Sometimes you may want to look up a type in a union type based on a property.

In this challenge, we want to get the corresponding type by searching the public type field in the union type Cat | Dog.

In other words, in the following example, we expect LookUp to get Dog and LookUp to get Cat .

Answer

Medium , #template-literal

Implements TrimLeft , which receives a determined string type and returns a new string, where The newly returned string has the blank string at the beginning of the original string removed.

Answer

Medium , #template-literal

Implements Trim which is a string type and returns a new string with two All whitespace characters at the end have been removed.

Answer

Medium , #template-literal

Implements Capitalize which converts the first letter of the string to uppercase, leaving the remaining letters as is .

Answer

Medium , #template-iteral

Implement Replace to replace the first substring From in string S Replace with To.

Answer

Medium , #template-literal

Implement ReplaceAll to replace all substrings From in a string S for To.

Answer

Medium , #arguments

Implement a generic AppendArgument for a given function type Fn and an arbitrary type A , returns a new function G. G takes all the parameters of Fn and appends parameters of type A at the end.

Answer

Medium , #union

Implement the full permutation of the union type and convert the union type into the union type of all possible fully permuted arrays.

Answers

/type-challenges/type-challenges/issues/614

Note

Medium , #template-literal

p>

Calculates the length of a string, similar to String#length.

Answer

Medium , #array

In this challenge, you need to write a type that accepts an array and returns a flattened array type.

Answer

Medium , #object-keys

Implement a type that adds a new field to the interface. This type receives three parameters and returns an interface type with new fields.

Answer

Medium , #math , #template-literal

Implement an Absolute type that accepts string, number or bigInt type parameters and returns a positive character string.

Answer

Medium , #union , #string

Implement a type that converts the received String parameter into a letter Union.

Answer

Medium , #object

Implement the full permutation of the union type and convert the union type into the union type of all possible fully permuted arrays.

Answer

Medium , #

FooBarBaz -> foo-bar-baz

Answer

Medium , #object

Get the difference attribute in two interface types.

Answer

Medium , #array

Implemented in a type system similar to the any function in Python. The type takes an array and returns true if any element in the array is true, otherwise it returns false. If the array is empty, returns false .

Answer

Medium , #union , #utils

Implement the IsNever type, parse the input T type as never and return true, otherwise return false

Answer

Medium , #union , #utils

Implement the IsUnion type, parse the input T type as a union type and return true, otherwise return false

Answer

Medium

Implements the ReplaceKeys type, which will replace the key value of the type in the union type. If the type does not have this Key, it will be skipped. If there is, it will be replaced.

Answer

Medium

Exclude index signatures from object types.

Answer

/type-challenges/type-challenges/issues/3542

Medium

Implements type PercentageParser. Matches type T according to the rule /^(\+|\-)?(\d*)?(\%)?$/.

The matching result consists of three parts, namely: [positive and negative sign, number, unit]. If there is no match, the default is an empty string.

Answer

Medium

Removes specified characters from a string.

Answer

Medium , Math

Given a positive integer as a type parameter, the return type is required to be the number minus 1.

Answer

Medium , object

Select attributes of the same type from F

Answer

Medium , #object

Retain fields of types not specified in U

Answer

Medium , #template-literal

Implement StartsWith , receives two string type parameters, then determines whether T starts with U, and returns true or false according to the result

Answer

Medium , #object

Implement a generic PartialByKeys which receives two type parameters T and K.

K specifies the set of properties of T that should be set to optional. When K is not provided, it behaves like a normal Partial making all properties optional.

Answer

Medium , #object

Implement a generic RequiredByKeys that receives two type parameters T and K .

K specifies the set of attributes of T that should be made required. When K is not provided, it behaves like a normal Required making all properties required.

Answer

Medium , #readonly , object-keys

Implement a general type Mutable to make all properties of type T mutable (not read only).

Answer

Medium , #object

1

Answer

Medium

Given a tuple containing only strings, and type U, recursively construct the object

Answer

Medium , #tuple

Implement the typed version of array inversion Array.reverse

Answer

Medium , #arguments

Implement type version of lodash _.flip function

Type FlipArguments Requires function T and returns a new function type. This function type has the same parameters, but the parameter types are reversed.

Answer

Medium , #array

Flatten the array recursively by depth.

Answer

Medium

Block, Element, Modifier Method (BEM) is a popular naming convention for classes in CSS. For example, a block component would be represented as btn , elements that depend on the block would be represented as btn__price , and modifiers that change the style of a block would be represented as btn--big or btn__price--warning . Implements BEM to generate a string union from these three arguments. where B is a string literal and E and M are string arrays (can be empty).

Answer

Medium , #object

A type version that implements in-order traversal of a binary tree.

Answer

Medium

Implementation type just-flip-object:

Answer

Medium

Implement generic Fibonacci Pass in the number T and return the correct Fibonacci number.

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

Answer

Medium

Implements type AllCombinations to return all character combinations.

Answer

Medium, #array

Implements type GreaterThan to compare sizes, like T > U. Negative numbers do not need to be considered

1

Answer

Medium , #tuple

Implements the Zip type. T, U must be Tuple

Answer

Medium , #tuple

Implement IsTuple, receive type T and determine whether T is a tuple type

< p> Answer

Medium, #tuple

Implement Chunk, which has two required type parameters, T must be tuple, N must be greater than 1 Number

Answer

Medium , tuple

Fill , a commonly used JavaScript function, we use types to implement it. Fill , it receives 4 type parameters, T and N are required parameters, T is a tuple, N is any, Start and End are optional parameters, which are numbers greater than zero.

In order to simulate the real Function, the test may contain some boundary conditions, I hope you like it:)

Answer

Medium

Implement TrimRight It uses precise characters String type and returns a new string with trailing whitespace removed.

Answer

Medium , #union , #array

Implement a generic Without like Lodash.without function, which receives an array If type T and number or array type U are parameters, an array T with the elements in U removed will be returned.

Answer

Medium , template-literal

Implements the typed version of Math.trunc . It accepts a string or number and returns the integer part, raising the decimal part

p>

Answer

Medium , #array

Implements the type version of Array.indexOf , which receives arrays T and U and returns the index of U in T Value

Answer

Medium , #array

Implementation type version Array.join receives array T and string or number U

Answer

Medium , #array

Implements the type version of Array.lastIndexOf , which receives arrays T and U and returns the reverse of U in T Index value

Answer

Medium , #array

Implements the type version of Lodash.uniq, which receives the array T and returns the deduplicated T

Answer

Medium

Implements MapTypes which converts the types in object T into different types defined by type R, which has the following structure.

Answer

Medium , #tuple

Constructs a tuple of given length

Answer

Medium

Sometimes we want to limit the range of numbers...eg.

Answer

Medium , #array , #application , #string

Given an array of strings, perform permutations and combinations. It is also useful for types like video controlsList

Answer

Medium , #union

Given an array of unique elements, return all possible subsequences .

A subsequence is a sequence that can be derived from an array by removing some elements or not removing any elements without changing the order of the remaining elements.

Answer