Skip to content

Commit 2a2ae51

Browse files
author
Ryan Johnson
authored
Feature/add typescript def (#22)
* add typescript support
1 parent d303f77 commit 2a2ae51

File tree

4 files changed

+133
-2
lines changed

4 files changed

+133
-2
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
"main": "./lib/index.js",
66
"module": "./es/index.js",
77
"jsnext:main": "./es/index.js",
8+
"typings": "lib/index.d.ts",
89
"files": [
910
"*.md",
1011
"es",
1112
"lib",
1213
"dist"
1314
],
1415
"scripts": {
15-
"build": "npm run build:commonjs && npm run build:es && npm run build:flow && npm run build:umd && npm run build:umd:min",
16+
"build": "npm run build:commonjs && npm run build:es && npm run build:flow && npm run build:umd && npm run build:umd:min && npm run copy:ts",
1617
"build:commonjs": "rimraf lib && cross-env BABEL_ENV=commonjs babel ./src -d lib",
1718
"build:es": "rimraf es && cross-env BABEL_ENV=es babel ./src -d es",
1819
"build:umd": "rimraf dist && webpack --env.dev --output-filename dist/ReactLocalizeRedux.js",
1920
"build:umd:min": "webpack --env.prod --output-filename dist/ReactLocalizeRedux.min.js",
2021
"build:flow": "flow-copy-source ./src lib && flow-copy-source ./src es",
22+
"copy:ts": "ncp ./src/index.d.ts ./lib/index.d.ts && ncp ./src/index.d.ts ./es/index.d.ts",
2123
"coverage": "jest --coverage",
2224
"prepublish": "npm run build",
2325
"start": "webpack-dev-server --config examples/webpack.config.babel.js --content-base examples --inline --open",
@@ -79,6 +81,7 @@
7981
"html-webpack-plugin": "^2.24.1",
8082
"jest": "^20.0.4",
8183
"json-loader": "^0.5.4",
84+
"ncp": "^2.0.0",
8285
"progress-bar-webpack-plugin": "^1.9.1",
8386
"react": "^15.6.1",
8487
"react-addons-test-utils": "^15.6.0",

src/index.d.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { ReactElement } from 'react';
2+
import { Selector } from 'reselect';
3+
import { ComponentClass } from 'react-redux';
4+
5+
export as namespace ReactLocalizeRedux;
6+
7+
export interface Language {
8+
code: string;
9+
active: boolean;
10+
}
11+
12+
export interface Translations {
13+
[key: string]: string[];
14+
}
15+
16+
export interface Options {
17+
renderInnerHtml?: boolean;
18+
defaultLanguage?: string;
19+
}
20+
21+
export interface LocaleState {
22+
languages: Language[];
23+
translations: Translations;
24+
options: Options;
25+
}
26+
27+
export interface TranslatedLanguage {
28+
[key: string]: string;
29+
}
30+
31+
export type LocalizedElement = ReactElement<'span'>|string;
32+
33+
export interface LocalizedElementMap {
34+
[key: string]: LocalizedElement;
35+
}
36+
37+
export interface TranslatePlaceholderData {
38+
[key: string]: string|number;
39+
}
40+
41+
export type TranslateValue = string|string[];
42+
43+
interface BaseAction<T, P> {
44+
type: T;
45+
payload: P;
46+
}
47+
48+
export type Translate = (value: TranslateValue, data: TranslatePlaceholderData, options?: Options) => LocalizedElement|LocalizedElementMap;
49+
50+
type InitializePayload = {
51+
languageCodes: string[],
52+
options?: Options
53+
};
54+
55+
type AddTranslationPayload = {
56+
translation: Object
57+
};
58+
59+
type AddTranslationForLanguagePayload = {
60+
translation: Object,
61+
language: string
62+
};
63+
64+
type SetLanguagesPayload = {
65+
languageCodes: string[],
66+
activeLanguage?: string
67+
};
68+
69+
type SetActiveLanguagePayload = {
70+
languageCode: string
71+
};
72+
73+
type LocalizeProps = {
74+
currentLanguage: string,
75+
translate: Translate
76+
};
77+
78+
export type InitializeAction = BaseAction<'@@localize/INITIALIZE', InitializePayload>;
79+
export type AddTranslationAction = BaseAction<'@@localize/ADD_TRANSLATION', AddTranslationPayload>;
80+
export type AddTranslationForLanguageAction = BaseAction<'@@localize/ADD_TRANSLATION_FOR_LANGUGE', AddTranslationForLanguagePayload>;
81+
export type SetActiveLanguageAction = BaseAction<'@@localize/SET_ACTIVE_LANGUAGE', SetActiveLanguagePayload>;
82+
export type SetLanguagesAction = BaseAction<'@@localize/SET_LANGUAGES', SetLanguagesPayload>;
83+
84+
export type Action = BaseAction<
85+
string,
86+
& InitializePayload
87+
& AddTranslationPayload
88+
& AddTranslationForLanguagePayload
89+
& SetActiveLanguagePayload
90+
& SetLanguagesPayload
91+
>;
92+
93+
export type ActionLanguageCodes = Action & { languageCodes: string[] };
94+
95+
export interface LocalizeStateProps {
96+
currentLanguage: string;
97+
translate: Translate;
98+
}
99+
100+
export function localeReducer(state: LocaleState, action: Action): LocaleState;
101+
102+
export function initialize(languageCodes: string[], options: Options): InitializeAction;
103+
104+
export function addTranslation(translation: Object): AddTranslationAction;
105+
106+
export function addTranslationForLanguage(translation: Object, language: string): AddTranslationForLanguageAction;
107+
108+
export function setLanguages(languageCodes: string[], activeLanguage: string): SetLanguagesAction;
109+
110+
export function setActiveLanguage(languageCode: string): SetActiveLanguageAction;
111+
112+
export function getTranslations(state: LocaleState): Translations;
113+
114+
export function getLanguages(state: LocaleState): Language[];
115+
116+
export function getOptions(state: LocaleState): Options;
117+
118+
export function getActiveLanguage(state: LocaleState): Language;
119+
120+
export function getTranslate(state: LocaleState): Selector<LocaleState, Translate>;
121+
122+
export function localize(Component: ReactElement<any>, slice?: string): (state: LocaleState) => ComponentClass<LocalizeProps>;

src/modules/locale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export const localeReducer = (state: LocaleState = initialState, action: Action)
194194
/**
195195
* ACTION CREATORS
196196
*/
197-
export const initialize = (languageCodes: string[], options: Options = defaultTranslateOptions) => ({
197+
export const initialize = (languageCodes: string[], options: Options = defaultTranslateOptions): InitializeAction => ({
198198
type: INITIALIZE,
199199
payload: { languageCodes, options }
200200
});

0 commit comments

Comments
 (0)