Skip to content

Conversation

amir-khoshbakht
Copy link

Description

as you can see there is a union type which consists of two strings "aaa" and "bbb".
I need to get sure that class property is one of allowed values. This is done in three ways in the following section.
The first method: does not have the necessary efficiency and even though there is no "bad" option in the type, it does not show an error

the second method: In this case, all the options should be available in the type ((unionType)), and this case is desirable

import { IsEnum} from 'class-validator'; type unionType = 'aaa' | 'bbb'; const as = <T>(option: T) => option; class ForTest{ // method 1 @IsEnum(['aaa', 'bad'] as unionType[]) test: string; // method 2 // typescript dosnt allow "bad" string which is not available in type @IsEnum(as<unionType[]>(['aaa', 'bad'])) test2: string; }

References

the expected method :

// typescript will show error for "bad" value @IsEnum<unionType[]>(['aaa', 'bad']) test3: string;

If the generic type is added to these functions, the correctness of the type can be ensured directly without defining the secondary function.

note: function "as" is custom function above
image

typestack-ci
typestack-ci previously approved these changes May 5, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 5, 2023
@typestack-ci
Copy link

@dependabot squash and merge

@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 8, 2023
typestack-ci
typestack-ci previously approved these changes May 8, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 8, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 9, 2023
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes Apr 22, 2024
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 2, 2024
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 6, 2024
@typestack-ci
Copy link

@dependabot squash and merge

@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes May 7, 2024
typestack-ci
typestack-ci previously approved these changes May 8, 2024
@typestack-ci
Copy link

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes Jun 16, 2024
@typestack-ci
Copy link

@dependabot squash and merge

haiweilian and others added 5 commits November 26, 2024 14:42
* fix: fill base64 options correctly pass the constraint to the isBase64 function add unit test IsBase64 options * refactor: pass options by name --------- Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
* fix: update version in package-lock.json * fix: pass options to isBase64 correctly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet