Skip to content

isDate() type guard #1907

Closed
Closed
@BarryThePenguin

Description

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

Activity

imballinst

imballinst commented on Aug 22, 2020

@imballinst
Contributor

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

icehaunter commented on Aug 24, 2020

@icehaunter

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

maximillianfx commented on Aug 24, 2020

@maximillianfx

Thanks @icehaunter , I'll check it out.

imballinst

imballinst commented on Aug 25, 2020

@imballinst
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

adenvt commented on Apr 14, 2021

@adenvt

any update for this issue??

imballinst

imballinst commented on Apr 14, 2021

@imballinst
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

tan75 commented on Apr 14, 2021

@tan75
Contributor

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

dodas

dodas commented on Jun 23, 2021

@dodas

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

fturmel

fturmel commented on Jun 23, 2021

@fturmel
Member

@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

fturmel commented on Jun 23, 2021

@fturmel
Member

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

RuckieOfficial commented on Nov 17, 2023

@RuckieOfficial

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      isDate() type guard · Issue #1907 · date-fns/date-fns