2017-03-27 20 views
7

में एक Moment.js ऑब्जेक्ट एनोटेशन निर्दिष्ट करने के लिए कैसे मैं वर्तमान में इसे मौजूदा प्रोजेक्ट पर लागू करके फ्लो सीख रहा हूं और एक क्षण के रूप में फ़ंक्शन पैरामीटर को एनोटेट करना चाहता हूं। जेएस ऑब्जेक्ट्स। जब मैं Moment.JS वस्तुओं के रूप में फ़ंक्शन पैरामीटर टिप्पणी करने की कोशिशफ़्लो

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; 
    ... 

हालांकि:

flow-typed का उपयोग करते हुए मैं प्रकार मैं तलाश कर रहा हूँ के लिए प्रकट होता है, जिसमें Moment.JS के लिए एक पुस्तकालय परिभाषा स्थापित करने में सक्षम था , फ्लो उन्हें इस तरह पहचानने में विफल रहता है। निम्नलिखित फ़ंक्शन में startDate और endDate क्षण हैं। जेएस दिनांक ऑब्जेक्ट्स।

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

प्रवाह के साथ यह और भी संभव है:

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

प्रवाह निम्न त्रुटि देता है? या क्या मुझे type को क्षण के लिए डुप्लिकेट करने की आवश्यकता है। जेएस ऑब्जेक्ट फ्लो-टाइप द्वारा प्रदान की गई लाइब्रेरी परिभाषा में से एक के समान है? मैं ऐसा नहीं करना चाहूंगा क्योंकि libdef काफी लंबा है।

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 => {...}; 

मैं क्या याद आ रही है:

उदाहरण के लिए

?

उत्तर

12

आपके इच्छित प्रकार को प्राप्त करने के तीन तरीके हैं।

यदि आपकी फ़ाइल पहले से ही की moment एक मॉड्यूल के रूप की आवश्यकता होती है आप या उस प्रकार

import moment from 'moment'; 

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

का उपयोग कर सकेंगे यदि आप स्रोत, लेकिन आपकी फ़ाइल के भीतर सिर्फ प्रकार का उपयोग नहीं करते चाहिए।

import type Moment from 'moment'; 

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

आप क्योंकि है कि क्या libdef मॉड्यूल निर्यात के रूप में निर्दिष्ट करता है यह कर सकते हैं: https://github.com/flowtype/flow-typed/blob/7822da72587078a4b8e0f2b56746d0da41a3ddde/definitions/npm/moment_v2.x.x/flow_v0.34.x-/moment_v2.x.x.js#L233

वैकल्पिक रूप से, ऐसा लगता है कि कि libdef भी ग्लोबल नेम स्पेस में एक प्रकार moment$Moment वाणी ताकि आप उस का उपयोग कर सकते हैं।

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

मैं वैश्विक उपयोग की अनुशंसा नहीं करता हालांकि यह कम स्पष्ट है कि यह कहां से आ रहा है।

+0

'पल $ पल' मेरे लिए काम नहीं करेगा, शायद क्योंकि मैं वेबपैक का उपयोग कर रहा हूं इसलिए कोई वैश्विक नामस्थान नहीं है? दो आयात विधियों ने काम किया। – clhenrick

+0

वेबपैक का प्रवाह प्रवाह पर असर नहीं होना चाहिए - मुझे यकीन नहीं है कि वैश्विक प्रकार आपके लिए क्यों काम नहीं कर रहा है लेकिन यह ठीक है क्योंकि आपको वास्तव में इसका उपयोग नहीं करना चाहिए ... CONTRIBUTING.md में प्रवाह-टाइप के लिए दस्तावेज़, यह libdef लेखकों को ग्लोबल्स को उपसर्ग करने के लिए कहता है जो वैश्विक होने के लिए नहीं हैं: (https://github.com/flowtype/flow-typed/blob/316320bf2cbc48a9447e053272a5c113e6e26a8c/CONTRIBUTING.md#prefix-global-variables -यह-वास्तव में-मतलब-से-होने-वैश्विक नहीं है), इसलिए आपको इसका उपयोग नहीं करना चाहिए :) –

0

ऐसा लगता है के रूप में पल वस्तु से एक उपवर्ग बनाने यदि इस समस्या का हल:

import moment from 'moment'; 

class Moment extends moment {} 

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

अभी भी रुचि इस करता है, तो टिप्पणी करने के लिए एक Moment.JS तथापि आपत्ति सही तरीका पता है।

संबंधित मुद्दे