From 2b9d93ec88f7da5e427ab7f81910dbdf7946e2e5 Mon Sep 17 00:00:00 2001 From: krister Date: Mon, 12 Jun 2017 21:57:51 +0300 Subject: [PATCH 1/2] Enable linting --- types/react-intl/index.d.ts | 64 +++++++++++++-------------- types/react-intl/react-intl-tests.tsx | 28 +++++++----- types/react-intl/tslint.json | 1 + 3 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 types/react-intl/tslint.json diff --git a/types/react-intl/index.d.ts b/types/react-intl/index.d.ts index 9777987c8d..644e9c9450 100644 --- a/types/react-intl/index.d.ts +++ b/types/react-intl/index.d.ts @@ -1,22 +1,22 @@ -// Type definitions for react-intl 2.2 +// Type definitions for react-intl 2.3 // Project: http://formatjs.io/react/ // Definitions by: Bruno Grieder , // Christian Droulers , // Fedor Nezhivoi , // Till Wolff , // Karol Janyst , -// Brian Houser +// Brian Houser , +// Krister Kari // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 declare namespace ReactIntl { - type DateSource = Date | string | number; interface Locale { locale: string; - fields?: { [key: string]: string }, - pluralRuleFunction?: (n: number, ord: boolean) => string; + fields?: { [key: string]: string }; + pluralRuleFunction?(n: number, ord: boolean): string; } type LocaleData = Locale[]; @@ -26,7 +26,7 @@ declare namespace ReactIntl { withRef?: boolean; } - type ComponentConstructor

= React.ComponentClass

| React.StatelessComponent

+ type ComponentConstructor

= React.ComponentClass

| React.StatelessComponent

; function injectIntl

(component: ComponentConstructor

, options?: InjectIntlConfig): React.ComponentClass

& { WrappedComponent: ComponentConstructor

}; @@ -34,7 +34,7 @@ declare namespace ReactIntl { function addLocaleData(data: Locale[] | Locale): void; interface Messages { - [key: string]: FormattedMessage.MessageDescriptor + [key: string]: FormattedMessage.MessageDescriptor; } function defineMessages(messages: T): T; @@ -64,19 +64,19 @@ declare namespace ReactIntl { const intlShape: IntlShape; interface InjectedIntl { - formatDate: (value: DateSource, options?: FormattedDate.PropsBase) => string; - formatTime: (value: DateSource, options?: FormattedTime.PropsBase) => string; - formatRelative: (value: DateSource, options?: FormattedRelative.PropsBase & { now?: any }) => string; - formatNumber: (value: number, options?: FormattedNumber.PropsBase) => string; - formatPlural: (value: number, options?: FormattedPlural.Base) => keyof FormattedPlural.PropsBase; - formatMessage: (messageDescriptor: FormattedMessage.MessageDescriptor, values?: {[key: string]: string | number}) => string; - formatHTMLMessage: (messageDescriptor: FormattedMessage.MessageDescriptor, values?: {[key: string]: string}) => string; + formatDate(value: DateSource, options?: FormattedDate.PropsBase): string; + formatTime(value: DateSource, options?: FormattedTime.PropsBase): string; + formatRelative(value: DateSource, options?: FormattedRelative.PropsBase & { now?: any }): string; + formatNumber(value: number, options?: FormattedNumber.PropsBase): string; + formatPlural(value: number, options?: FormattedPlural.Base): keyof FormattedPlural.PropsBase; + formatMessage(messageDescriptor: FormattedMessage.MessageDescriptor, values?: {[key: string]: string | number}): string; + formatHTMLMessage(messageDescriptor: FormattedMessage.MessageDescriptor, values?: {[key: string]: string}): string; locale: string; formats: any; messages: { [id: string]: string }; defaultLocale: string; defaultFormats: any; - now : () => number; + now(): number; } interface InjectedIntlProps { @@ -90,9 +90,9 @@ declare namespace ReactIntl { } namespace FormattedDate { - export interface PropsBase extends IntlComponent.DateTimeFormatProps {} + type PropsBase = IntlComponent.DateTimeFormatProps; - export interface Props extends PropsBase { + interface Props extends PropsBase { value: DateSource; } } @@ -100,16 +100,16 @@ declare namespace ReactIntl { class FormattedDate extends React.Component { } namespace FormattedTime { - export interface PropsBase extends IntlComponent.DateTimeFormatProps {} + type PropsBase = IntlComponent.DateTimeFormatProps; - export interface Props extends PropsBase { + interface Props extends PropsBase { value: DateSource; } } class FormattedTime extends React.Component { } namespace FormattedRelative { - export interface PropsBase { + interface PropsBase { /* * one of "second", "minute", "hour", "day", "month" or "year" */ @@ -123,7 +123,7 @@ declare namespace ReactIntl { initialNow?: any; } - export interface Props extends PropsBase { + interface Props extends PropsBase { value: DateSource; } } @@ -131,13 +131,13 @@ declare namespace ReactIntl { class FormattedRelative extends React.Component { } namespace FormattedMessage { - export interface MessageDescriptor { + interface MessageDescriptor { id: string; description?: string; defaultMessage?: string; } - export interface Props extends MessageDescriptor { + interface Props extends MessageDescriptor { values?: {[key: string]: string | number | JSX.Element}; tagName?: string; } @@ -147,26 +147,25 @@ declare namespace ReactIntl { class FormattedHTMLMessage extends React.Component { } namespace FormattedNumber { - export interface PropsBase extends Intl.NumberFormatOptions { + interface PropsBase extends Intl.NumberFormatOptions { format?: string; } - export interface Props extends PropsBase { + interface Props extends PropsBase { value: number; } } class FormattedNumber extends React.Component { } - namespace FormattedPlural { - export interface Base { + interface Base { /* * one of "cardinal" (default) | "ordinal" */ style?: "cardinal" | "ordinal"; } - export interface PropsBase extends Base { + interface PropsBase extends Base { other?: any; zero?: any; one?: any; @@ -175,15 +174,14 @@ declare namespace ReactIntl { many?: any; } - export interface Props extends PropsBase { + interface Props extends PropsBase { value: number; } } class FormattedPlural extends React.Component { } - namespace IntlProvider { - export interface Props { + interface Props { locale?: string; formats?: any; messages?: any; @@ -196,12 +194,12 @@ declare namespace ReactIntl { class IntlProvider extends React.Component { getChildContext(): { intl: InjectedIntl; - } + }; } } declare module "react-intl" { - export = ReactIntl + export = ReactIntl; } declare module "react-intl/locale-data/af" { diff --git a/types/react-intl/react-intl-tests.tsx b/types/react-intl/react-intl-tests.tsx index 8b21ef8651..42305ef571 100644 --- a/types/react-intl/react-intl-tests.tsx +++ b/types/react-intl/react-intl-tests.tsx @@ -3,8 +3,8 @@ * Updated by Fedor Nezhivoi */ -import * as React from "react" -import * as reactMixin from "react-mixin" +import * as React from "react"; +import * as reactMixin from "react-mixin"; import { IntlProvider, @@ -21,14 +21,14 @@ import { FormattedPlural, FormattedDate, FormattedTime -} from "react-intl" +} from "react-intl"; import reactIntlEn = require("react-intl/locale-data/en"); addLocaleData(reactIntlEn); interface SomeComponentProps { - className: string + className: string; } const SomeFunctionalComponentWithIntl: React.ComponentClass = injectIntl(({ @@ -51,7 +51,10 @@ const SomeFunctionalComponentWithIntl: React.ComponentClass const formattedNumber = formatNumber(123, { format: "short" }); const formattedPlural = formatPlural(1, { style: "ordinal" }); const formattedMessage = formatMessage({ id: "hello", defaultMessage: "Hello {name}!" }, { name: "Roger" }); - const formattedMessagePlurals = formatMessage({ id: "hello", defaultMessage: "Hello {name} you have {unreadCount, number} {unreadCount, plural, one {message} other {messages}}!" }, { name: "Roger", unreadCount: 123 }); + const formattedMessagePlurals = formatMessage({ + id: "hello", + defaultMessage: "Hello {name} you have {unreadCount, number} {unreadCount, plural, one {message} other {messages}}!" }, + { name: "Roger", unreadCount: 123 }); const formattedHTMLMessage = formatHTMLMessage({ id: "hello", defaultMessage: "Hello {name}!" }, { name: "Roger" }); return (

@@ -71,7 +74,10 @@ class SomeComponent extends React.Component{name}!" }, { name: "Roger" }); return
{formattedNum} )} -
+
; } } @@ -230,14 +236,14 @@ const SomeComponentWithIntl = injectIntl(SomeComponent); class TestApp extends React.Component<{}, {}> { render(): React.ReactElement<{}> { const definedMessages = defineMessages({ - "sup": { + sup: { id: "sup", defaultMessage: "Hai mom" } }); const messages = { - "hello": "Hello, {name}!" + hello: "Hello, {name}!" }; return ( @@ -250,9 +256,9 @@ class TestApp extends React.Component<{}, {}> { const intlProvider = new IntlProvider({ locale: 'en' }, {}); const { intl } = intlProvider.getChildContext(); -const wrappedComponent = +const wrappedComponent = ; export default { TestApp, SomeComponent: SomeComponentWithIntl -} +}; diff --git a/types/react-intl/tslint.json b/types/react-intl/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-intl/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 81e990d1d742f9f2a4e8f1b9cf787624a77327fd Mon Sep 17 00:00:00 2001 From: krister Date: Mon, 12 Jun 2017 22:08:09 +0300 Subject: [PATCH 2/2] Allow booleans, dates and numbers when formatting a message --- types/react-intl/index.d.ts | 4 ++-- types/react-intl/react-intl-tests.tsx | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/types/react-intl/index.d.ts b/types/react-intl/index.d.ts index 644e9c9450..f52fbc1cbc 100644 --- a/types/react-intl/index.d.ts +++ b/types/react-intl/index.d.ts @@ -69,8 +69,8 @@ declare namespace ReactIntl { formatRelative(value: DateSource, options?: FormattedRelative.PropsBase & { now?: any }): string; formatNumber(value: number, options?: FormattedNumber.PropsBase): string; formatPlural(value: number, options?: FormattedPlural.Base): keyof FormattedPlural.PropsBase; - formatMessage(messageDescriptor: FormattedMessage.MessageDescriptor, values?: {[key: string]: string | number}): string; - formatHTMLMessage(messageDescriptor: FormattedMessage.MessageDescriptor, values?: {[key: string]: string}): string; + formatMessage(messageDescriptor: FormattedMessage.MessageDescriptor, values?: {[key: string]: string | number | boolean | Date}): string; + formatHTMLMessage(messageDescriptor: FormattedMessage.MessageDescriptor, values?: {[key: string]: string | number | boolean | Date}): string; locale: string; formats: any; messages: { [id: string]: string }; diff --git a/types/react-intl/react-intl-tests.tsx b/types/react-intl/react-intl-tests.tsx index 42305ef571..1550aafcbe 100644 --- a/types/react-intl/react-intl-tests.tsx +++ b/types/react-intl/react-intl-tests.tsx @@ -74,11 +74,17 @@ class SomeComponent extends React.Component{name}!" }, { name: "Roger" }); + const formattedHTMLMessageNumber = intl.formatHTMLMessage({ id: "hello", defaultMessage: "Hello {num}!" }, { num: 1 }); + const formattedHTMLMessageDate = intl.formatHTMLMessage({ id: "hello", defaultMessage: "Hello {date}!" }, { date: new Date() }); + const formattedHTMLMessageBool = intl.formatHTMLMessage({ id: "hello", defaultMessage: "Hello {bool}!" }, { bool: true }); return