synacor / preact-i18n

Simple localization for Preact.
BSD 3-Clause "New" or "Revised" License
205 stars 18 forks source link

Add initial typings #16

Closed kokjinsam closed 5 years ago

kokjinsam commented 6 years ago

Related: #12

billneff79 commented 6 years ago

Thanks for the PR @sammkj .

It looks like it is missing the function definitions for intl and withText. Did you leave those out on purpose.

Also, I'm not super familiar with typescript, but preact defines a typings.json file and it also packages the .d.ts file as part of the build steps in package.json, which this PR doesn't do. My guess is that this PR isn't quite complete enough to have this work for the general public yet.

billneff79 commented 6 years ago

Build steps from preact: https://github.com/developit/preact/blob/master/package.json#L13-L14

kokjinsam commented 6 years ago

@billneff79 , I'm not using any of those two APIs at the moment. But I can write definitions for it. Let me make some changes.

kokjinsam commented 6 years ago

@billneff79 , I need some help here. Here's what I got so far for withText.

  // withText("foo,bar,baz")(App)
  export function withText<P, S, M>(
    mapping: string
  ): (Child: Preact.AnyComponent<P & M, S>) => Preact.FunctionalComponent<P>;

  // withText(["foo", "bar", "baz"])(App)
  export function withText<P, S, M>(
    mapping: Array<string>
  ): (Child: Preact.AnyComponent<P & M, S>) => Preact.FunctionalComponent<P>;

  // withText({foo: "foo", bar: "bar", baz: "baz"})(App)
  export function withText<P, S, M>(
    mapping: M
  ): (Child: Preact.AnyComponent<P & M, S>) => Preact.FunctionalComponent<P>;

usage:

class App extends Component<{ placeholder: string }, {}> {
  render() {
    return (
      <div>
        <h1>Hello</h1>
      </div>
    );
  }
}

const Meow = withText<{}, {}, { placeholder: string }>("placeholder")(App);

But it keeps throwing the following error:

Argument of type 'typeof App' is not assignable to parameter of type 'AnyComponent<{ placeholder: string; }, {}>'.
  Type 'typeof App' is not assignable to type 'ComponentConstructor<{ placeholder: string; }, {}>'.
    Type 'App' is not assignable to type 'Component<{ placeholder: string; }, {}>'.
      Types of property 'props' are incompatible.
        Type '{ placeholder: string; } & ComponentProps<App>' is not assignable to type '{ placeholder: string; } & ComponentProps<Component<{ placeholder: string; }, {}>>'.
          Type '{ placeholder: string; } & ComponentProps<App>' is not assignable to type 'ComponentProps<Component<{ placeholder: string; }, {}>>'.
            Types of property 'ref' are incompatible.
              Type '((el: App) => void) | undefined' is not assignable to type '((el: Component<{ placeholder: string; }, {}>) => void) | undefined'.
                Type '(el: App) => void' is not assignable to type '((el: Component<{ placeholder: string; }, {}>) => void) | undefined'.
                  Type '(el: App) => void' is not assignable to type '(el: Component<{ placeholder: string; }, {}>) => void'.
                    Types of parameters 'el' and 'el' are incompatible.
                      Type 'Component<{ placeholder: string; }, {}>' is not assignable to type 'App'.
                        Types of property 'render' are incompatible.
                          Type '(props?: ({ placeholder: string; } & ComponentProps<Component<{ placeholder: string; }, {}>>) | u...' is not assignable to type '() => Element'.
                            Type 'Element | null' is not assignable to type 'Element'.
                              Type 'null' is not assignable to type 'Element'.

Any ideas how to solve this?

EDITS

Functional Component works with any problem. Only Class component that is causing problem.

jtrollia commented 5 years ago

You should either merge this or mention https://www.npmjs.com/package/@types/preact-i18n in the README 👌The latter works fine for me.

billneff79 commented 5 years ago

@jtrollia - I'll close this and reference the definitely typed typings