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

add typescript generic type to functions to ensure type check #2075

Open
wants to merge 396 commits into
base: master
Choose a base branch
from

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
Copy link

@dependabot squash and merge

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

@dependabot squash and merge

typestack-ci
typestack-ci previously approved these changes Apr 12, 2024
@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
Copy link

@dependabot squash and merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet