Skip to content
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

Closed
acomagu opened this issue Jan 25, 2023 · 12 comments
Closed

Feature request: Introduce TypeScript Assertion function #469

acomagu opened this issue Jan 25, 2023 · 12 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers hacktoberfest help wanted Extra attention is needed question Further information is requested
Projects

Comments

@acomagu
Copy link

acomagu commented Jan 25, 2023

Feature Request

Any plan to introduce function using TypeScript assertion feature?

For example:

export declare function assert2<T>(input: unknown): asserts input is T;

Previously, the value of the asserted type had to be assigned to a new variable.

const typedValue = assert<Type>(value);

But with this function, a new variable is no longer needed.

assert2<Type>(value);
// now value is typed

I hope this introduced! Thank you.

@samchon
Copy link
Owner

samchon commented Feb 12, 2023

It may hard to introduce in formal ts guidebook

@samchon samchon closed this as completed Feb 12, 2023
@parsadotsh
Copy link

@samchon Sorry, can you explain why would this be hard to introduce? It would greatly add to the already amazing library.

@samchon
Copy link
Owner

samchon commented Jul 31, 2023

@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.

If you ask the reason why to the documentation repo (https://github.com/microsoft/TypeScript-Website), I think core team member may explain much detailed than me.

@acomagu
Copy link
Author

acomagu commented Aug 1, 2023

@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);

@samchon
Copy link
Owner

samchon commented Aug 1, 2023

Ah, you mean combinig both is<T>() and assert<T>() function.

It's not easy because typia should be compatible with typescript-is.

At least, you'd better to recommend much better name than assert2

@samchon samchon self-assigned this Aug 1, 2023
@samchon samchon reopened this Aug 1, 2023
@samchon samchon added help wanted Extra attention is needed good first issue Good for newcomers question Further information is requested labels Aug 1, 2023
@acomagu
Copy link
Author

acomagu commented Oct 29, 2023

@samchon How about guard(), asserts() or assertGuard()?

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 assert<T>(input: unknown): T is almost never more handy than assert<T>(input: unknown): asserts input is T(who would want reassignment?), so it's ideal to change the existing assert method to use the asserts syntax in the next major update. However, if backward compatibility is a priority, choosing another name(guard) would also be a good idea.

(From the perspective of compatibility with typescript-is, leaving assertType as it is should be enough because typescript-is doesn't have assert method.)

@samchon
Copy link
Owner

samchon commented Oct 30, 2023

microsoft/TypeScript#56256

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 assert<T>() function and take a major update, or create a new validation function like assertGuard<T>(). I think this is the right way. Thanks for long term waiting and good suggestion.

@samchon samchon added documentation Improvements or additions to documentation enhancement New feature or request hacktoberfest labels Oct 30, 2023
@samchon samchon added this to To do in V5 Update via automation Oct 30, 2023
@samchon
Copy link
Owner

samchon commented Nov 12, 2023

Tested the type assertion to the createAssert() function, it has critical problem like below.

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

image

image

samchon added a commit that referenced this issue Nov 12, 2023
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.
@acomagu
Copy link
Author

acomagu commented Nov 13, 2023

Thank you for investigation!!
Hmm, I understand the problem. Indeed, if this is a TypeScript spec, it would not be possible to implement createAssert.

But if it is, is it possible to introduce the feature as a function has other name(like assertGuard)? In that case, although createAssertGuard can't be implemented now, user would be able to choose favorite strategy, and we could implement it after TypeScript have supported such higher-order function with asserts clause.

asserts function seems minor feature, but I think that it is an important feature that would really make typia more handy. Since we(users of typia) cannot wrap the existing assert function myself to complement this feature, we hope that it will be implemented in some form in the core of typia.

(This is just a thought, is it difficult commonalize the implementation of all assert<T>() for same T automatically? Instead of inlining the whole implementation at the place, put the implementation into typia module(by transforming typia code itself), and call it from the usage place)

@samchon
Copy link
Owner

samchon commented Nov 16, 2023

This feature (assertGuard) would be supported after TypeScript v5.3 release with ts-patch supporting.

samchon added a commit that referenced this issue Nov 17, 2023
Added new function `typia.assertGuard<T>()` function which performs the assertion guard of TypeScript.
@samchon
Copy link
Owner

samchon commented Nov 17, 2023

Install the next version, then you can use the assertGuard() function earlier.

npm install typia@next 

samchon added a commit that referenced this issue Nov 17, 2023
samchon added a commit that referenced this issue Nov 17, 2023
Complete #469 - new function `asserGuard()`.
@acomagu
Copy link
Author

acomagu commented Nov 19, 2023

Biggest Thanks!!!

I love typia so much

@acomagu acomagu closed this as completed Nov 19, 2023
V5 Update automation moved this from To do to Done Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers hacktoberfest help wanted Extra attention is needed question Further information is requested
Projects
No open projects
Development

No branches or pull requests

3 participants