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

Typescript: Observer prop type is not extracting #102

Open
quolpr opened this issue Dec 10, 2020 · 2 comments
Open

Typescript: Observer prop type is not extracting #102

quolpr opened this issue Dec 10, 2020 · 2 comments

Comments

@quolpr
Copy link

quolpr commented Dec 10, 2020

Giving:

type Props = { note: NoteModel; database: Database; noteId: string };

const NotePageComponent: React.FC<Props> = ({ note }) => {
  const database = useDatabase();
  const history = useHistory();

  return (
    <>
      {note && (
        <Calendar
          onChange={async (date) => {
            if (isArray(date)) return;

            const note = await getOrCreateDailyNote(database, dayjs(date));

            history.replace(`/notes/${note.id}`);
          }}
          value={dayjs(note.title, 'D MMM YYYY').toDate()}
        />
      )}
      <Note noteId={note.id} />
    </>
  );
};

type InputProps = ObservableifyProps<Props, 'note'>;

const NotePageEnch = withDatabase<InputProps>(
  withObservables(['noteId'], ({ database, noteId }: InputProps) => {
    const note = database.collections
      .get<NoteModel>(HarikaNotesTableName.NOTES)
      .findAndObserve(noteId);

    return {
      note,
    };
})(/* error here ->>> */NotePageComponent)
);

I get such typescript error:

TS2345: Argument of type 'FC<Props>' is not assignable to parameter of type 'ComponentType<Matching<ExtractedObservables<{ note: Observable<Note>; }>, Props>>'.
  Type 'FunctionComponent<Props>' is not assignable to type 'FunctionComponent<Matching<ExtractedObservables<{ note: Observable<Note>; }>, Props>>'.
    Types of property 'propTypes' are incompatible.
      Type 'WeakValidationMap<Props> | undefined' is not assignable to type 'WeakValidationMap<Matching<ExtractedObservables<{ note: Observable<Note>; }>, Props>> | undefined'.
        Type 'WeakValidationMap<Props>' is not assignable to type 'WeakValidationMap<Matching<ExtractedObservables<{ note: Observable<Note>; }>, Props>>'.
          Types of property 'note' are incompatible.
            Type 'Validator<Note> | undefined' is not assignable to type 'Validator<Observable<Note>> | undefined'.
              Type 'Validator<Note>' is not assignable to type 'Validator<Observable<Note>>'.
                Type 'Note' is not assignable to type 'Observable<Note>'.
Version: typescript 4.0.5

Typescript version 4.0.5

@quolpr quolpr changed the title Observer prop type is not extracting Typescript: Observer prop type is not extracting Dec 10, 2020
@blixit
Copy link

blixit commented Apr 3, 2021

I'm getting the same issue but with a different pattern

import {Database} from '@nozbe/watermelondb';
import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables, {ExtractedObservables} from '@nozbe/with-observables';
import React from 'react';
import {withTheme} from 'react-native-paper';
import {Theme} from 'react-native-paper/lib/typescript/types';
import {Observable} from 'rxjs';

import {AnonymousRoute} from '../screen/navigator.types';

import { AndonSubmission } from './model/andonSubmission';

export interface WithObservablesInput {
  database: Database;
  route: AnonymousRoute<{id: string}>;
  theme: Theme;
}

export function withTRDOInjected<ObservablesProps>(
  WrappedComponent: React.FC<WithObservablesInput & ExtractedObservables<ObservablesProps>>,
  deps: Array<keyof WithObservablesInput>,
  getObservables: (props: WithObservablesInput) => ObservablesProps,
) {
  const observe = withObservables(deps, getObservables);

  return withTheme(withDatabase(observe(WrappedComponent))); // <<--- KO
}

Here is the result:
image

But if I replace the generic type with a known type it works perfectly ...
image

Anyway, in both cases, my component is properly injected without typescript error:
image

I know I'm close to the result ... but I don't know how to bypass this typescript error

@mlecoq
Copy link

mlecoq commented Nov 8, 2021

Same issue and it was due to 2 versions of RxJS in my dependencies : WatermelonDB used 7.x and WithObservables 6.x

Adding

  "resolutions": {
    "rxjs": "^7.3.0"
  },

solves the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants