// Type definitions for fluent-react 0.8 // Project: http://projectfluent.org // Definitions by: Huy Nguyen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.3 import * as React from 'react'; import { FluentBundle, } from 'fluent'; export interface Node { TEXT_NODE: 3; nodeType: number; localName?: string; textContext: string; } export type MarkupParser = (str: string) => Node[]; export interface Context { l10n: ReactLocalization; parseMarkup: MarkupParser; } export interface LocalizationProviderProps { bundles: IterableIterator; parseMarkup?: MarkupParser; } export class LocalizationProvider extends React.Component { } export class ReactLocalization { constructor(bundles: IterableIterator); getString(id: string, args?: object, fallback?: string): string; } export interface LocalizedProps { id: string; attrs?: object; [key: string]: any; } export class Localized extends React.Component { } // Inspired by react-redux's type definition: /** * A property P will be present if: * - it is present in DecorationTargetProps * * Its value will be dependent on the following conditions * - if property P is present in InjectedProps and its definition extends the definition * in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P] * - if property P is not present in InjectedProps then its definition will be that of * DecorationTargetProps[P] * - if property P is present in InjectedProps but does not extend the * DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P] */ export type Matching = { [P in keyof DecorationTargetProps]: P extends keyof InjectedProps ? InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : InjectedProps[P] : DecorationTargetProps[P]; }; /** * a property P will be present if : * - it is present in both DecorationTargetProps and InjectedProps * - InjectedProps[P] can satisfy DecorationTargetProps[P] * ie: decorated component can accept more types than decorator is injecting * * For decoration, inject props or ownProps are all optionally * required by the decorated (right hand side) component. * But any property required by the decorated component must be satisfied by the injected property. */ export type Shared< InjectedProps, DecorationTargetProps > = { [P in Extract]?: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never; }; // Infers prop type from component C export type GetProps = C extends React.ComponentType ? P : never; export type GetString = (id: string, args?: object) => string; export interface InjectedProps { getString: GetString; } // Taken from // https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#predefined-conditional-types export type Omit = Pick>; // Injects `getString` and removes it from the prop requirements. Will not pass // through `getString` if it's passed in during render. export function withLocalization>>>( component: C ): React.ComponentType, keyof Shared>>>;