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

isDate() type guard #1907

Closed
BarryThePenguin opened this issue Aug 18, 2020 · 11 comments
Closed

isDate() type guard #1907

BarryThePenguin opened this issue Aug 18, 2020 · 11 comments

Comments

@BarryThePenguin
Copy link

BarryThePenguin commented Aug 18, 2020

Is it possible to add a type guard for isDate() in the TypeScript definitions?

I looked at previous issues and couldn't see any previous discussion. It doesn't look like it was considered in the implementation #754

- function isDate(value: any): boolean
+ function isDate(value: any): value is Date

I looked at putting a PR together, but wasn't confident making changes to the script that builds the type definitions

@imballinst
Copy link
Contributor

imballinst commented Aug 22, 2020

hi @BarryThePenguin!

I think this may be related but I might be wrong -- @kossnocorp is working on this typescriptify branch. When it's finished, this isDate function https://github.com/date-fns/date-fns/blob/typescriptify/src/isDate/index.ts will have value: Date as the parameter, so is guaranteed to be a Date object. CMIIW @kossnocorp.

@icehaunter
Copy link

I filed a similar issue about isValid: #1880. @maximillianfx wanted to tackle that, so I am mentioning him.

@imballinst, the file you linked still returns boolean, which means it might not act as a typeguard. TS might infer that from value instanceof Date, I didn't test, but I think that it would be better to be explicit. TS doc specifies type guard as value is Date, which would imply boolean return, but also would mark the passed-in argument as a Date for that branch of code.

Moreover, for the code you've linked, I would specify the incoming type as value: unknown to not rely on implicit any, but that is slightly beyond the current discussion

@maximillianfx
Copy link

Thanks @icehaunter , I'll check it out.

@imballinst
Copy link
Contributor

TS doc specifies type guard as value is Date, which would imply boolean return, but also would mark the passed-in argument as a Date for that branch of code.

@icehaunter ah, gotcha! Yes, that is a great point, I didn't see that before. I 💯 agree with the part of being explicit, it's really useful for assertions in a code-branch.

@adenvt
Copy link

adenvt commented Apr 14, 2021

any update for this issue??

@imballinst
Copy link
Contributor

This should be fixed in the recent Typescript-ifying effort, I think?

export default function isDate(value: unknown): value is Date {
requiredArgs(1, arguments)
return (
value instanceof Date ||
(typeof value === 'object' &&
Object.prototype.toString.call(value) === '[object Date]')
)
}

@tan75
Copy link
Contributor

tan75 commented Apr 14, 2021

@adenvt @imballinst
thats correct - this function has already been migrated to TS.

@tan75 tan75 closed this as completed Apr 14, 2021
@dodas
Copy link

dodas commented Jun 23, 2021

Hey @tan75 !
When is this expected to be released? Looks like it isn't out in 2.21.1.
Thanks.

@fturmel
Copy link
Member

fturmel commented Jun 23, 2021

@dodas @tan75

I just investigated this. While the type guard is implemented in the isDate source code, the TS types published on npm for this project are generated with a custom system based off the jsdocs definitions.

Some changes would have to be made to that system so it understands TS type guards but doesn't mess up documentation or Flow types generation. Maybe by introducing a custom jsdoc tag that would take precedent over @returns for TS types only? Or have that system understand that TS type guards are equivalent to booleans for docs/Flow?

* @param {*} value - the value to check
* @returns {boolean} true if the given value is a date

export default function isDate(value: unknown): value is Date {

function isDate(value: any): boolean

@fturmel
Copy link
Member

fturmel commented Jun 23, 2021

Also regarding #1880, I don't think isValid should implement a similar type guard since the argument passed could be a numerical timestamp and return true.

See the truth table in the docs: https://date-fns.org/docs/isValid

@RuckieOfficial
Copy link

RuckieOfficial commented Nov 17, 2023

Why is it closed? It still returns boolean instead of Date object.

In this generated file is boolean returned:
https://github.com/date-fns/date-fns/blob/typescriptify/src/isDate/index.ts#L36

and in this definition it is fine:

export default function isDate(value: unknown): value is Date {
requiredArgs(1, arguments)
return (
value instanceof Date ||
(typeof value === 'object' &&
Object.prototype.toString.call(value) === '[object Date]')
)
}

It is probably due to this typescript generate definition: https://github.com/date-fns/date-fns/blob/b500440132b69fdf2b430743cf86bbc929b60307/scripts/build/_lib/typings/typeScript.js#L142C57-L142C57

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

No branches or pull requests

9 participants