Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support intl.get(key) type check #83

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/typescript-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "react-intl-universal-typescript-example",
"version": "1.0.0",
"private": true,
"author": "[email protected]",
"license": "BSD-3-Clause",
"devDependencies": {
"react-scripts": "^0.9.0"
},
"dependencies": {
"react-intl-universal": "*",
"typescript": "^3.0.3"
},
"scripts": {
"tsc": "tsc"
}
}
6 changes: 6 additions & 0 deletions examples/typescript-example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import intl from 'react-intl-universal';
intl.get('sign')
intl.get('invalid');
intl.getHTML('sign');
intl.get('invalid');
intl.get('sign');
3 changes: 3 additions & 0 deletions examples/typescript-example/src/locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sign": "sign"
}
3 changes: 3 additions & 0 deletions examples/typescript-example/src/locales/zh.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sign": "登录"
}
20 changes: 20 additions & 0 deletions examples/typescript-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"sourceMap": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"jsx": "preserve",
"module": "commonjs",
"target": "es2015",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"lib": ["es2017", "dom", "dom.iterable"],
"noEmit": true,
"pretty": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"strict": true
},
"include": ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
4 changes: 4 additions & 0 deletions examples/typescript-example/typings/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as en from '../src/locales/en.json';
declare module 'react-intl-universal' {
export interface Message extends Record<keyof typeof en, string> {}
}
18 changes: 12 additions & 6 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
declare module "react-intl-universal" {
/**
* Intl translation type definition
* @example { 'sign': string, 'logout': string }
*/
export interface Message {}
type Key = keyof Message;
/**
* Helper: determine user's locale via URL, cookie, and browser's language.
* You may not this API, if you have other rules to determine user's locale.
Expand Down Expand Up @@ -33,7 +39,7 @@ declare module "react-intl-universal" {
* @param {string} key The string representing key in locale data file
* @returns {string} message
*/
export function get(key: string): string;
export function get(key: Key): string;

/**
* Get the formatted message by key
Expand All @@ -48,18 +54,18 @@ declare module "react-intl-universal" {
* @param {string} key The string representing key in locale data file
* @returns {React.Element} message
*/
export function getHTML(key: string): string;
export function getHTML(key: Key): string;

/**
* Get the formatted html message by key.
* @param {string} key The string representing key in locale data file
* @param {Object} variables Variables in message
* @returns {React.Element} message
*/
export function getHTML(key: string, value: any): string;
export function getHTML(key: Key, value: any): string;

/**
* Get the inital options
* Get the inital options
* @returns {Object} options includes currentLocale and locales
*/
export function getInitOptions(): ReactIntlUniversalOptions;
Expand All @@ -76,7 +82,7 @@ declare module "react-intl-universal" {

/**
* Load more locales after init
* @param {Object} locales App locale data
* @param {Object} locales App locale data
*/
export function load(locales: { [key: string]: any }): void;

Expand All @@ -88,7 +94,7 @@ declare module "react-intl-universal" {
urlLocaleKey?: string;
warningHandler?: (message?: any, error?: any) => void;
}

export interface ReactIntlUniversalMessageDescriptor {
id: string,
defaultMessage?: string,
Expand Down