Skip to content

Instantly share code, notes, and snippets.

@Yogu
Last active September 11, 2018 15:10
Show Gist options
  • Save Yogu/f3ed40d2d6fb3e4ad0b2b76983536053 to your computer and use it in GitHub Desktop.
Save Yogu/f3ed40d2d6fb3e4ad0b2b76983536053 to your computer and use it in GitHub Desktop.
format-message typings (outdated, see this PR: https://github.com/format-message/format-message/pull/206)
declare module 'format-message-parse' {
namespace parse {
export type AST = Element[]
export type Element = string | Placeholder
export type Placeholder = Plural | Styled | Typed | Simple
export type Plural = [string, 'plural' | 'selectordinal', number, SubMessages]
export type Styled = [string, string, string | SubMessages]
export type Typed = [string, string]
export type Simple = [string]
export interface SubMessages {
[key: string]: AST
}
interface Context {
pattern: string,
index: number,
tagsType?: string,
tokens?: Token[]
}
export type Token = [TokenType, string]
export type TokenType = 'text' | 'space' | 'id' | 'type' | 'style' | 'offset' | 'number' | 'selector' | 'syntax'
export class SyntaxError extends Error {
expected: string | undefined;
found: string | undefined;
offset: number;
line: number;
public column: number;
constructor(message: string, expected: string | undefined, found: string | undefined, offset: number, line: number, column: number)
}
}
function parse(pattern: string, options?: { tagsType?: string, tokens?: parse.Token[] }): parse.AST;
export = parse;
}
declare module 'format-message-interpret' {
import { AST, Placeholder } from 'format-message-parse';
namespace interpret {
type Locale = string
type Locales = Locale | Locale[]
export type Type = (placeholder: Placeholder, locales: Locales) => (context: any, params?: object) => any
export interface Types {
[key: string]: Type
}
export function toParts(ast: AST, locale?: Locales, type?: Types): (args?: object) => any[];
export const types: Types;
}
function interpret(ast: AST, locale?: interpret.Locales, types?: interpret.Types): (args?: object) => string;
export = interpret;
}
declare module 'format-message-print' {
import { AST } from 'format-message-parse';
function print(ast: AST): string;
export = print;
}
declare module 'format-message' {
import { Types } from 'format-message-interpret';
type Locale = string
type Locales = Locale | Locale[]
type Message = string | {
id?: string,
default: string,
description?: string
}
interface Translations {
[key: string]: { [key: string]: string | Translation } | undefined
}
interface Translation {
message: string,
format?: (args?: object) => string,
toParts?: (args?: object) => any[],
}
type Replacement = string | null | undefined | ((a: string, b: string, locales?: Locales) => string | undefined)
type GenerateId = (source: string) => string
type MissingTranslation = 'ignore' | 'warning' | 'error'
interface FormatObject {
[key: string]: any
}
interface Options {
locale?: Locales,
translations?: Translations,
generateId?: GenerateId,
missingReplacement?: Replacement,
missingTranslation?: MissingTranslation,
formats?: {
number?: FormatObject,
date?: FormatObject,
time?: FormatObject
},
types?: Types
}
interface Setup {
locale: Locales,
translations: Translations,
generateId: GenerateId,
missingReplacement: Replacement,
missingTranslation: MissingTranslation,
formats: {
number: FormatObject,
date: FormatObject,
time: FormatObject
},
types: Types
}
interface FormatMessage {
(msg: Message, args?: object, locales?: Locales): string,
rich(msg: Message, args?: object, locales?: Locales): any[],
setup(opt?: Options): Setup,
number(value: number, style?: string, locales?: Locales): string,
date(value: number | Date, style?: string, locales?: Locales): string,
time(value: number | Date, style?: string, locales?: Locales): string,
select(value: any, options: object): any,
custom(placeholder: any[], locales: Locales, value: any, args: object): any,
plural(value: number, offset: any, options: any, locale: any): any,
selectordinal(value: number, offset: any, options: any, locale: any): any,
namespace(): FormatMessage
}
const formatMessage: FormatMessage;
export = formatMessage;
}
declare module 'message-format' {
import { Types } from 'format-message-interpret';
import { AST, Placeholder } from 'format-message-parse';
interface Options {
types: Types
}
interface Internals {
ast: AST,
format: (args?: object) => string,
locale: string,
locales?: string | string[],
toParts?: (args?: object) => any[],
options?: Options
}
class MessageFormat {
format: (args?: object) => string;
formatToParts: (args?: object) => Placeholder;
resolvedOptions: () => { locale: string | string[] | undefined };
supportedLocalesOf: (requestedLocales?: string | string[]) => string[];
constructor(pattern: string, locales?: string | string[], options?: Options);
}
export = MessageFormat;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment