kakasoo

typia.random<Generic>은 불가능 본문

프로그래밍/TypeScript

typia.random<Generic>은 불가능

카카수(kakasoo) 2023. 5. 24. 00:10
반응형
it('predict', { only: true }, async () => {
    type SomeGenericType<T extends string> = T extends '' ? never : string;
    class TestClass<T extends string = '', P = SomeGenericType<T>> {
        /**
         * Error: Error on typia.random(): non-specified generic argument.
         */
        predict1() {
            return typia.random<P>(); // complie error
        }

        /**
         * non-compile error, but if I call this `predict` method, it occur error.
         *
         * for example, `TypeError: $pick(...) is not a function`
         */
        predict2() {
            return typia.random<SomeGenericType<T>>();
        }
    }

    new TestClass<'testStringType'>().predict2(); // error.
});

https://github.com/samchon/typia/issues/125

 

Support for Generic Types · Issue #125 · samchon/typia

I am very interested in Application Transformer, and converting TS types into json-schema/openapi. I would like to be able to use TSON.application with a generic like so: function toJsonSchema<T> {...

github.com

 

불가능할 거라고 생각했지만 코드를 작성하는 시점과 컴파일 시점에서는 문제가 없는 것처럼 나와서 이슈를 남겼다.

predict1은 코드 작성에는 에러가 없지만 컴파일 시점부터 에러가 발생한다.

predict2는 컴파일 시점에는 에러가 없지만 런타임 시점부터 에러가 발생한다.

일관된 에러를 뱉는 것이 더 나을 거란 생각이 들어 일단 질문을 남겨놓았다.

반응형