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

Check if object is a function if the type idicates so #159

Merged
merged 3 commits into from
Nov 14, 2021
Merged

Check if object is a function if the type idicates so #159

merged 3 commits into from
Nov 14, 2021

Conversation

chomosuke
Copy link
Collaborator

@chomosuke chomosuke commented Nov 5, 2021

closes #155.

If a properties has a function type, it checks if the object is actually a function in the type guard.

It would also emit a warning regarding how the signature of the function will never be tested in the type guard. The warning can be suppressed by putting comment ts-auto-guard-suppress function-type before the declaration. I'm happy to change the warning or the way the warning is suppressed.

I tried to find out how to test the warning in console output with tape but to no avail. If anyone know how to do it please tell me or you tackle it yourself. Otherwise we'll probably have to make do with the fact that the emitted warning will not be tested.

@chomosuke chomosuke marked this pull request as ready for review November 5, 2021 20:03
@rhys-vdw
Copy link
Owner

Hey @chomosuke, looks good to me. Thanks for the contribution. Sorry for the delay, for some reason CI isn't running on external PRs so I had to find a moment to test it locally.

FWIW I think the warning is unnecessary, but happy to go with your preference on this one.

You've been invited as a collaborator. If you do accept there is no obligation on you to contribute, but I ask that you add test coverage to any PRs you open and try to get at least one other collaborator to review before merging.

@rhys-vdw rhys-vdw merged commit f0e1e31 into rhys-vdw:master Nov 14, 2021
@rhys-vdw
Copy link
Owner

It's not necessary to add test coverage, but if you want to check output you'd do it something like this:

https://javascript.plainenglish.io/how-to-test-stdout-output-in-node-js-6c36edc610d1

@chomosuke
Copy link
Collaborator Author

Hey @rhys-vdw, Thanks for the invite. I will probably add test coverage for the warning anyway just for good measure.

@pkyeck
Copy link

pkyeck commented Nov 15, 2022

I'm getting the warning:

It seems that typedObj["sanitize"] has a function type.
Note that it is impossible to check if a function has the correct signature and return type at runtime.
To disable this warning, put comment "ts-auto-guard-suppress function-type" before the declaration.

... but can't get rid of it with:

/** @see {isSchemaResult} ts-auto-guard:type-guard */
export type SchemaResult<T extends SchemaRule = SchemaRule> = {
  required: boolean;
  nullable?: boolean;
  rule: T;
  // ts-auto-guard-suppress function-type
  sanitize?: (val: any) => any;
  excludeOptional?: boolean;
};

Am I doing something wrong?

@rhys-vdw
Copy link
Owner

I think you need to put that comment above the type declaration, not the field @pkyeck. if that doesn't work please make a new issue, this one is closed.

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.

For function types, should at least check if the property is indeed a function.
3 participants