00002-medium-return-type

Back

type MyReturnType<T extends (...arg: any)=> any> = T extends (...arg: any)=> infer U ? U : never

Solution by chenweiCwCw #35102

type MyReturnType<T> = T extends (...args: never[]) => infer P ? P : never

Solution by ClarityOfMind #35038

// 你的答案

const fn = (v: boolean) => { if (v) { return 1 } else { return 2 } }

type MyReturnType = T extends (...arg: any) => infer B ? B : never

type resultType = MyReturnType

Solution by DisneyTom #35026

type MyReturnType<T extends (...args: any[]) => unknown> = T extends (...args: any[]) => infer R ? R : never;

Solution by dominikmatt #35006

type MyReturnType<T> = T extends (...arg: any[]) => infer R ? R : never;

Solution by raeyoung-kim #34985

// 你的答案
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;

Solution by showthesunli #34972

// your answers
type MyReturnType<T> = T extends (...args: any) => infer R ? R : never;

Solution by miju-Park #34970

type MyReturnType<T> = T extends (...args: any) => infer R ? R : never;

Solution by 56aiden90 #34900

type MyReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer Return ? Return : never;

Solution by eunsukimme #34819

// your answers
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : any;

Solution by zeyuanHong0 #34738

We can use infer keyword to find fn's return type.

type MyReturnType<T> = T extends (...params: any[]) => infer U ? U: never;

Solution by dev-jaemin #34583

type MyReturnType<T extends (...args: any[]) => unknown> = T extends (...args: (infer K)) => (infer U)
  ? U : false
// your answers

Solution by Rustamaha #34558

type MyReturnType<T> = T extends (...arg: never[]) => infer ReturnVal ? ReturnVal : never

Solution by devshinthant #34552

type MyReturnType<T> = T extends (...arg : any[]) => infer R ? R : never;

Solution by bkdragon0228 #34540

type MyReturnType<T extends (...arg: any[]) => unknown> = T extends (...arg: any[]) => infer P ? P : never

Solution by binhdv155127 #34491

// 여기 풀이를 입력하세요
type MyReturnType<T extends (...args: unknown[]) => unknown> = T extends (...args: unknown[]) => infer U ? U : never;

Solution by LeeKangHyun #34481

// your answers
type MyReturnType<T> = T extends (...args: any[]) => infer P  ?  P : never

Solution by gobielJonathan #34449

type MyReturnType<T> = T extends (...args: any[]) => infer P ? P : never;

Solution by wxh-cyber #34416

type MyReturnType<T> = T extends (...args: any) => infer P ? P : any

Solution by ktim816 #34412

type MyReturnType<T extends Function> = T extends (...args: any[]) => infer A ? A : never

Solution by rookie-luochao #34367

type MyReturnType<T> = T extends (...args: any) => infer R ? R : never

Solution by quitone #34199

T extends (...args: any) => infer R? R : never;

Solution by lijiayuan365 #34164

문제 설명

내장 제네릭 ReturnType<T>을 이를 사용하지 않고 구현하세요.

예시:

const fn = (v: boolean) => {
	if (v) return 1;
	else return 2;
};

type a = MyReturnType<typeof fn>; // should be "1 | 2"

풀이

type MyReturnType<T> = T extends (...args: any) => infer R ? R : never;

크게 어렵지 않았습니다. 다만 extends 뒤의 (...args)를 작성했어야 했는데 단일 parameter를 작성(args)하여 조금 문제가 발생했었습니다.

Solution

/* _____________ 여기에 코드 입력 _____________ */

type MyReturnType<T> = T extends (...args: any) => infer R ? R : never;

/* _____________ 테스트 케이스 _____________ */
import type { Equal, Expect } from "@type-challenges/utils";

type cases = [
	Expect<Equal<string, MyReturnType<() => string>>>,
	Expect<Equal<123, MyReturnType<() => 123>>>,
	Expect<Equal<ComplexObject, MyReturnType<() => ComplexObject>>>,
	Expect<Equal<Promise<boolean>, MyReturnType<() => Promise<boolean>>>>,
	Expect<Equal<() => "foo", MyReturnType<() => () => "foo">>>,
	Expect<Equal<1 | 2, MyReturnType<typeof fn>>>,
	Expect<Equal<1 | 2, MyReturnType<typeof fn1>>>
];

type ComplexObject = {
	a: [12, "foo"];
	bar: "hello";
	prev(): number;
};

Solution by adultlee #34126

type MyReturnType<T extends Function> = T extends (...args: any) => infer ReturnType ? ReturnType : never;

Solution by ProvorovOleksii #34088

// 你的答案
type MyReturnType<T> = T extends (...unknown: any) => infer R ? R : unknown;

Solution by yyl134934 #34080

type MyReturnType<T> =  T extends (...args: any) => infer R ? R : T;

Solution by G00syara #34039

type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never

Solution by ouzexi #33976

type MyReturnType<T extends (...args : any) => any> = T extends (...args : any) => infer R  ? R : never;

Solution by ckdwns9121 #33932

type MyReturnType<T> = T  extends (...args: any[]) => infer R ? R : never

Solution by lidongyun120398 #33914

type MyReturnType<T extends (...args: never[]) => any> = T extends (...args: never[]) => infer U ? U : never

Solution by IllegalCreed #33898