Includes
#
introductionImplement the JavaScript Array.includes
function in the type system. A type takes the two arguments. The output should be a boolean true
or false
.
For example
ts
type isPillarMen = Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>; // expected to be `false`
View on GitHubts
type isPillarMen = Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>; // expected to be `false`
#
start pointtsTry
/* _____________ Your Code Here _____________ */typeIncludes <T extends readonly any[],U > = any;Â/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Kars'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[1, 2, 3, 5, 6, 7], 7>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[1, 2, 3, 5, 6, 7], 4>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[1, 2, 3], 2>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[1, 2, 3], 1>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[{}], {a : 'A' }>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[boolean, 2, 3, 5, 6, 7], false>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[true, 2, 3, 5, 6, 7], boolean>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[false, 2, 3, 5, 6, 7], false>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[{a : 'A' }], { readonlya : 'A' }>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[{ readonlya : 'A' }], {a : 'A' }>, false>>];
take the challengetsTry
/* _____________ Your Code Here _____________ */typeIncludes <T extends readonly any[],U > = any;Â/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Kars'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[1, 2, 3, 5, 6, 7], 7>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[1, 2, 3, 5, 6, 7], 4>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[1, 2, 3], 2>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[1, 2, 3], 1>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[{}], {a : 'A' }>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[boolean, 2, 3, 5, 6, 7], false>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[true, 2, 3, 5, 6, 7], boolean>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[false, 2, 3, 5, 6, 7], false>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[{a : 'A' }], { readonlya : 'A' }>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Includes <[{ readonlya : 'A' }], {a : 'A' }>, false>>];
#
my solutionSpoiler warning // Click to reveal answer
tsTry
typeisEqual <X ,Y > = (<T >() =>T extendsX ? 1 : 2) extends <T >() =>T extendsY ? 1 : 2? true: false;typeIncludes <T ,U > =T extends [inferF , ...inferRest ]?isEqual <F ,U > extends true? true:Rest extends []? false:Includes <Rest ,U >: false;
tsTry
typeisEqual <X ,Y > = (<T >() =>T extendsX ? 1 : 2) extends <T >() =>T extendsY ? 1 : 2? true: false;typeIncludes <T ,U > =T extends [inferF , ...inferRest ]?isEqual <F ,U > extends true? true:Rest extends []? false:Includes <Rest ,U >: false;