type If<C extends boolean, T, F> = C extends boolean ? C extends true ? T : F : never;
Solution by gangnamssal #35485
type If<C extends boolean, T, F> = C extends true ? T : F;
Solution by RanungPark #35437
type If<C extends boolean, T, F> = C extends true ? T : F
Solution by awsssrD #35401
type If<C extends boolean, T, F> = C extends true ? T : F
Solution by gyeounjeong #35358
// 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
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