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

#104 @AssertType rejects a promise when used in async methods #105

Merged
merged 1 commit into from
Nov 1, 2021

Conversation

HitoriSensei
Copy link
Contributor

@HitoriSensei HitoriSensei commented Jun 30, 2021

I've prepared a PR addressing #104

Changes are reflected in tests and in readme:

async and Promise returning methods

AssertType can also work correctly with async methods, returning promise rejected with TypeGuardError

To enable this functionality, you need to emit decorators metadata for your TypeScript project.

{
    "compilerOptions": {
      "emitDecoratorMetadata": true
    }
}

Then AssertType will work with async methods and Promise returning methods automatically.

import { ValidateClass, AssertType } from 'typescript-is';

@ValidateClass()
class A {
    async method(@AssertType({ async: true }) value: number) {
        // You can safely use value as a number
        return value;
    }

    methodPromise(@AssertType({ async: true }) value: number): Promise<number> {
        // You can safely use value as a number
        return Promise.resolve(value);
    }
}

new A().method(42).then(value => value === 42 /* true */); 
new A().method('42' as any).catch(error => {
    // error will be of TypeGuardError type
})
new A().methodPromise('42' as any).catch(error => {
    // error will be of TypeGuardError type
})

If you want to throw synchronously for some reason, you can override the behaviour using with @AssertType({ async: false }):

import { ValidateClass, AssertType } from 'typescript-is';

@ValidateClass()
class A {
    async method(@AssertType({ async: false }) value: number) {
        // You can safely use value as a number
        return value;
    }
}

new A().method(42).then(value => value === 42 /* true */);
new A().method('42' as any); // will throw error

If you cannot or don't want to enable decorators metadata, you still make AssertType reject with promise using @AssertType({ async: true })

import { ValidateClass, AssertType } from 'typescript-is';

@ValidateClass()
class A {
    async method(@AssertType({ async: true }) value: number) {
        // You can safely use value as a number
        return value;
    }
}

@HitoriSensei HitoriSensei changed the title https://github.com/woutervh-/typescript-is/issues/104 #104 @AssertType rejects a promise when used in async methods Jun 30, 2021
@woutervh- woutervh- merged commit 6938d9d into woutervh-:master Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants