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

new: add TypeScript type definitions #161

Merged
merged 1 commit into from
May 11, 2017
Merged
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"conventional-changelog-cli": "^1.2.0",
"conventional-github-releaser": "^1.1.3",
"cross-spawn": "^5.0.1",
"dtslint": "^0.1.2",
"eslint": "^3.14.1",
"eslint-config-vue": "^2.0.2",
"eslint-loader": "^1.6.1",
Expand Down Expand Up @@ -72,6 +73,7 @@
"dist/vue-i18n.common.js",
"dist/vue-i18n.esm.js",
"src",
"types",
"decls"
],
"homepage": "https://github.com/kazupon/vue-i18n#readme",
Expand All @@ -85,6 +87,7 @@
"license": "MIT",
"main": "dist/vue-i18n.common.js",
"module": "dist/vue-i18n.esm.js",
"types": "types/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/kazupon/vue-i18n.git"
Expand All @@ -99,13 +102,14 @@
"docs:build": "node config/version.js && gitbook build ./gitbook ./docs",
"docs:install": "gitbook install ./gitbook",
"flow": "flow check",
"dtslint": "dtslint types",
"lint": "eslint src test config",
"release": "conventional-github-releaser -n ./node_modules/git-commit-message-convention/convention.js",
"sauce": "npm run sauce:coolkids && npm run sauce:ie && npm run sauce:mobile",
"sauce:coolkids": "karma start config/karma.sauce.conf.js -- 0",
"sauce:ie": "karma start config/karma.sauce.conf.js -- 1",
"sauce:mobile": "karma start config/karma.sauce.conf.js -- 2",
"test": "npm run lint && npm run flow && npm run test:cover && npm run test:e2e -- --env phantomjs",
"test": "npm run lint && npm run flow && npm run dtslint && npm run test:cover && npm run test:e2e -- --env phantomjs",
"test:cover": "BABEL_ENV=test karma start config/karma.cover.conf.js",
"test:e2e": "npm run build && node test/e2e/runner.js",
"test:unit": "BABEL_ENV=test karma start config/karma.unit.conf.js"
Expand Down
74 changes: 74 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as Vue from 'vue';
import { PluginFunction } from 'vue';

declare namespace VueI18n {
type Path = string;
type Locale = string;
type Values = any[] | { [key: string]: any };
type Choice = number;
type LocaleMessage = string | LocaleMessageObject | LocaleMessageArray;
interface LocaleMessageObject { [key: string]: LocaleMessage; }
interface LocaleMessageArray { [index: number]: LocaleMessage; }
interface LocaleMessages { [key: string]: LocaleMessageObject; }
type TranslateResult = string | LocaleMessageArray;

interface Formatter {
format(message: string, values: Values): string;
}

type MissingHandler = (locale: Locale, key: Path, vm?: Vue) => void;

// tslint:disable-next-line:interface-name
interface I18nOptions {
locale?: Locale;
fallbackLocale?: Locale;
messages?: LocaleMessages;
formatter?: Formatter;
missing?: MissingHandler;
fallbackRoot?: boolean;
sync?: boolean;
silentTranslationWarn?: boolean;
}
}

declare class VueI18n {
constructor(options?: VueI18n.I18nOptions)

readonly messages: VueI18n.LocaleMessages;

locale: VueI18n.Locale;
fallbackLocale: VueI18n.Locale;
missing: VueI18n.MissingHandler;
formatter: VueI18n.Formatter;
silentTranslationWarn: boolean;

t(key: VueI18n.Path, values?: VueI18n.Values): VueI18n.TranslateResult;
t(key: VueI18n.Path, locale: VueI18n.Locale, values?: VueI18n.Values): VueI18n.TranslateResult;
tc(key: VueI18n.Path, choice?: VueI18n.Choice, values?: VueI18n.Values): string;
tc(key: VueI18n.Path, choice: VueI18n.Choice, locale: VueI18n.Locale, values?: VueI18n.Values): string;
te(key: VueI18n.Path, locale?: VueI18n.Locale): boolean;

getLocaleMessage(locale: VueI18n.Locale): VueI18n.LocaleMessageObject;
setLocaleMessage(locale: VueI18n.Locale, message: VueI18n.LocaleMessageObject): void;
mergeLocaleMessage(locale: VueI18n.Locale, message: VueI18n.LocaleMessageObject): void;

static install: PluginFunction<never>;
static version: string;
}

declare module 'vue/types/vue' {
interface Vue {
readonly $i18n: VueI18n;
$t: typeof VueI18n.prototype.t;
$tc: typeof VueI18n.prototype.tc;
$te: typeof VueI18n.prototype.te;
}
}

declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
i18n?: VueI18n;
}
}

export = VueI18n;
97 changes: 97 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import * as Vue from 'vue';
import * as VueI18n from 'vue-i18n';
import { ComponentOptions } from 'vue';

/**
* VueI18n.install
*/
Vue.use(VueI18n);
VueI18n.install(Vue);

/**
* VueI18n.version
*/
VueI18n.version; // $ExpectType string

/**
* VueI18n Instance
*/
const locale = 'locale';
const key = 'key';
const value = 'value';
const i18n = new VueI18n({
locale,
fallbackLocale: locale,
messages: {
[locale]: {
[key]: value,
},
},
formatter: {
format(message, values) {
return message;
},
},
missing(locale, key, vm) {
},
fallbackRoot: false,
sync: true,
silentTranslationWarn: true,
});
i18n.messages[locale][key]; // $ExpectType LocaleMessage
i18n.locale; // $ExpectType string
i18n.fallbackLocale; // $ExpectType string
i18n.missing; // $ExpectType MissingHandler
i18n.formatter; // $ExpectType Formatter
i18n.silentTranslationWarn; // $ExpectType boolean
i18n.setLocaleMessage; // $ExpectType (locale: string, message: LocaleMessageObject) => void
i18n.getLocaleMessage; // $ExpectType (locale: string) => LocaleMessageObject
i18n.mergeLocaleMessage; // $ExpectType (locale: string, message: LocaleMessageObject) => void
// $ExpectType { (key: string, values?: { [key: string]: any; } | undefined): TranslateResult; (key: string, locale: string, values?: { [key: string]: any; } | undefined): TranslateResult; }
i18n.t;
// tslint:disable-next-line:max-line-length
// $ExpectType { (key: string, choice?: number | undefined, values?: { [key: string]: any; } | undefined): string; (key: string, choice: number, locale: string, values?: { [key: string]: any; } | undefined): string; }
i18n.tc;
// $ExpectType (key: string, locale?: string | undefined) => boolean
i18n.te;

/**
* Vue
*/
const vm = new Vue({
i18n,
});
vm.$i18n; // $ExpectType VueI18n
vm.$t(key); // $ExpectType TranslateResult
vm.$t(key, ['', 0, false, null, undefined]); // $ExpectType TranslateResult
vm.$t(key, { x: 'x' }); // $ExpectType TranslateResult
vm.$t(key, locale);
vm.$t(key, locale, ['', 0, false, null, undefined]); // $ExpectType TranslateResult
vm.$t(key, locale, { x: 'x' }); // $ExpectType TranslateResult
vm.$tc(key); // $ExpectType string
vm.$tc(key, 1); // $ExpectType string
vm.$tc(key, 1, []); // $ExpectType string
vm.$tc(key, 1, {}); // $ExpectType string
vm.$tc(key, 1, locale); // $ExpectType string
vm.$tc(key, 1, locale, []); // $ExpectType string
vm.$tc(key, 1, locale, {}); // $ExpectType string
vm.$te(key); // $ExpectType boolean
vm.$te(key, locale); // $ExpectType boolean

/**
* VueI18n
*/
{
let path: VueI18n.Path;
let locale: VueI18n.Locale;
let values: VueI18n.Values;
let choice: VueI18n.Choice;
let localeMessage: VueI18n.LocaleMessage;
let localeMessageObject: VueI18n.LocaleMessageObject;
let localeMessageArray: VueI18n.LocaleMessageArray;
let localeMessages: VueI18n.LocaleMessages;
let translateResult: VueI18n.TranslateResult;
let formatter: VueI18n.Formatter;
let missingHandler: VueI18n.MissingHandler;
let i18nOptions: VueI18n.I18nOptions;
}
20 changes: 20 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": ".",
"types": [],
"paths": {
"vue-i18n": [
"."
]
},
"noEmit": true,
"forceConsistentCasingInFileNames": true
}
}
3 changes: 3 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dtslint.json"
}
Loading