web-dev-qa-db-fra.com

Comment spécifier une annotation d'objet Moment.js dans Flow

J'apprends actuellement Flow en l'appliquant à un projet existant et en cherchant à annoter les paramètres de fonction sous forme d'objets Moment.JS. 

À l’aide de flow-typed j’ai pu installer une définition de bibliothèque pour Moment.JS qui semble correspondre au type que je cherche:

declare class moment$Moment {
  static ISO_8601: string;
  static (string?: string, format?: string|Array<string>, locale?: string, strict?: bool): moment$Moment;
  static (initDate: ?Object|number|Date|Array<number>|moment$Moment|string): moment$Moment;
  static unix(seconds: number): moment$Moment;
  static utc(): moment$Moment;
  ...

Toutefois, lorsque j'essaie d'annoter les paramètres de fonction en tant qu'objets Moment.JS, Flow ne les reconnaît pas en tant que tels. Dans la fonction suivante, startDate et endDate sont des objets de date Moment.JS.

const filterByDateWhereClause = (startDate: Moment, endDate: Moment): string => {...};

Flow donne l'erreur suivante:

const filterByDateWhereClause = (startDate: Moment, endDate: Moment): string =>
                                                 ^^^^^^ identifier `Moment`. Could not resolve name

Est-ce même possible avec Flow? Ou dois-je dupliquer la type pour l'objet Moment.JS identique à celui de la définition de bibliothèque fournie par le type de flux? Je préférerais ne pas le faire car le libdef est assez long. 

Par exemple:

declare class Moment {
  static ISO_8601: string;
  static (string?: string, format?: string|Array<string>, locale?: string, strict?: bool): moment$Moment;
  static (initDate: ?Object|number|Date|Array<number>|moment$Moment|string): moment$Moment;
  static unix(seconds: number): moment$Moment;
  static utc(): moment$Moment;
  ...

const filterByDateWhereClause = (startDate: Moment, endDate: Moment): string => {...};

Qu'est-ce que je rate?

15
clhenrick

Il semble que créer une sous-classe à partir du moment où l'objet résout ce problème:

import moment from 'moment';

class Moment extends moment {}

const filterByDateWhereClause = (startDate: Moment, endDate: Moment): string => {...};

Toujours intéressé de savoir si c’est la bonne façon d’annoter un objet Moment.JS cependant.

0
clhenrick