-
-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Introduce TypeScript Assertion function #469
Comments
It may hard to introduce in formal ts guidebook |
@samchon Sorry, can you explain why would this be hard to introduce? It would greatly add to the already amazing library. |
@lucashyper Well, it is hard to explain why. Anyway, this is a 3rd party library, and the handbook is not introducing any type of 3rd party library.
|
@samchon I am sorry if my poor English caused your misunderstanding. I'm not talking about any handbook. I would like to see Assertion Function introduced in typia, like: typia.assert2<Type>(value); |
Ah, you mean combinig both It's not easy because At least, you'd better to recommend much better name than |
@samchon How about export declare function guard<T>(input: unknown): asserts input is T;
function example(input: unknown) {
guard<string>(input);
input.toLowerCase(); // input is string here.
} IMO (From the perspective of compatibility with typescript-is, leaving |
export function assertString(input: unknown): (asserts input is string) & string {
if (typeof input !== 'string')
throw new Error('Input is not a string');
return input;
} I've tried above way, but it is not working. If TypeScript team does not have any plan to support above statement, I'll follow your advise. Changing |
Tested the type assertion to the I'll ask this is bug or spec to the Microsoft repo. If it is spec, then no way to support this issue's suggestion \o/ - @acomagu @parsadotsh |
Tried to change `typia.createAssert<T>()` function to return `asserts input is T`, but it was not possible to accomplish the requirement due to TypeScript bug. I'll ask it to the TypeScript repo, and if it is a spec that Microsoft TypeScript team had intended, then no way to support that issue. ```typescript const constAssert = (input: unknown): asserts input is string => { if (typeof input !== 'string') throw new Error('Assertion failed'); }; function functionAssert(input: unknown): asserts input is string { if (typeof input !== 'string') throw new Error('Assertion failed'); } functionAssert('hello'); constAssert('hello'); ``` > `Assertions require every name in the call target to be declared with an explicit type annotation.
Thank you for investigation!! But if it is, is it possible to introduce the feature as a function has other name(like
(This is just a thought, is it difficult commonalize the implementation of all |
This feature ( |
Added new function `typia.assertGuard<T>()` function which performs the assertion guard of TypeScript.
Install the next version, then you can use the npm install typia@next |
Complete #469 - new function `asserGuard()`.
Biggest Thanks!!! I love typia so much |
Feature Request
Any plan to introduce function using TypeScript assertion feature?
For example:
Previously, the value of the asserted type had to be assigned to a new variable.
But with this function, a new variable is no longer needed.
I hope this introduced! Thank you.
The text was updated successfully, but these errors were encountered: