00268-easy-if

Back

// answers, This is more complete
type If<C extends boolean, T, F> = C extends true
  ? T
  : C extends boolean
  ? F
  : never;

// if type C = If<null, "a", "b">;  No alarm
// You need to enable "strictNullChecks": true in tsconfig.json

Solution by ren-142326 #35256

// 你的答案
type If<C extends boolean, T, F> = C extends true ? T : F

Solution by HoseaGuo #35129

type If<C, A, B> = C extends true ? A : B;

Solution by ClarityOfMind #34989

type If<C extends boolean, T, F> = C extends true ? T : F;

Solution by raeyoung-kim #34940

type If<C extends boolean, T, F> = C extends true ? T : F

Solution by 56aiden90 #34880

type If<C extends true | false, T, F> = C extends true ? T : F

Solution by eunsukimme #34813

type If<C extends boolean, T, F> = C extends true ? T : F

Solution by kang-kibong #34746

type If<C, T, F> = C extends true? T:F;

Solution by lephuthuc2001 #34710

type If<C extends Boolean, T, F> = C extends true ? T : F
image

Solution by Git-I985 #34703

// your answers
type If<C extends boolean, T, F> = C extends true ? T : F

Solution by zeyuanHong0 #34692

type If<C, T, F> = C extends true ? T : C extends false ? F : C | F

Solution by nathan2slime #34660

type If<C extends boolean, T, F> = C extends true ? T : F

Solution by Hailey0930 #34639

type If<C extends boolean, T, F> = C extends true ? T : F

Solution by Yunjaejo #34593

type If<C, T, F> = C extends boolean ? (C extends true ? T : F) : never;

Solution by huzi0324 #34559

/*
  268 - If
  -------
  by Pavel Glushkov (@pashutk) #easy #utils

  ### Question

  Implement the util type `If<C, T, F>` which accepts condition `C`, a truthy value `T`, and a falsy value `F`. `C` is expected to be either `true` or `false` while `T` and `F` can be any type.

  For example:

  ```ts
  type A = If<true, 'a', 'b'>  // expected to be 'a'
  type B = If<false, 'a', 'b'> // expected to be 'b'

View on GitHub: https://tsch.js.org/268 */

/* _____________ Your Code Here _____________ */

type If<C extends boolean, T, F> = C extends true ? T : F;

/* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils'

type cases = [ Expect<Equal<If<true, 'a', 'b'>, 'a'>>, Expect<Equal<If<false, 'a', 2>, 2>>, Expect<Equal<If<boolean, 'a', 2>, 'a' | 2>>, ]

// @ts-expect-error type error = If<null, 'a', 'b'>

/* _____________ Further Steps _____________ / /

Share your solutions: https://tsch.js.org/268/answer View solutions: https://tsch.js.org/268/solutions More Challenges: https://tsch.js.org */

Solution by ifeelonely #34554

type If<C, T, F> = C extends true ? T : F

Solution by devshinthant #34545

// 여기 풀이를 입력하세요
type If<C extends boolean, T, F> = C extends true ? T : F;

Solution by LeeKangHyun #34458

type If<C extends boolean, T, F> = C extends true ? T : F;

Solution by ktim816 #34424

// your answers
type If<C, T, F> = C extends true ? T: F

Solution by gobielJonathan #34395

type If<C extends boolean, T, F> = C extends true ? T : F

Solution by rookie-luochao #34359

type If<C extends boolean, T, F> = C extends true ? T : F;

考虑到报错的情况,C必须先为boolean类型,然后再在二者中进行选择。

Solution by wxh-cyber #34345

type If<C extends boolean, T, F> = C extends true ? T : F;

Solution by bkdragon0228 #34291

type If<A, B, C> = A extends true ? B : C;

Solution by souzaramon #34269

// 你的答案

type If<C extends boolean, T, F> = C extends true ?T:F

Solution by W-fitTiger #34247

type If<C extends boolean, T, F> = C extends true ? T : F

Solution by quitone #34192

Only true of false can be type of C, and we can make If type just from extends


type If<C extends true | false, T, F> = C extends true ? T : F

Solution by dev-jaemin #34159

type If<C extends boolean, T, F> = C extends true ? T : F

Solution by ouzexi #33969

type If<C extends boolean, T, F> = C extends true ? T : F

Solution by iam1maker #33954

type If<C extends boolean, T, F> = C extends true? T:F

Solution by hyojinLee-git #33943

type Falsey = null | undefined | false | "" | 0 | 0n | -0;
type If<Conditions, TrueType, FalseType> = Conditions extends Falsey ? FalseType :  TrueType

Solution by sakakibara1 #33940