Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

fix(typescript): fix query typings #32

Merged
merged 2 commits into from
Jun 20, 2019
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
9 changes: 5 additions & 4 deletions typings/get-queries-for-element.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ReactTestRenderer } from 'react-test-renderer';

import * as queries from './queries';
import { NativeTestInstance } from './query-helpers';

export type BoundFunction<T> = T extends (
attribute: string,
Expand All @@ -17,10 +18,10 @@ export type BoundFunctions<T> = { [P in keyof T]: BoundFunction<T[P]> };
interface Query extends Function {
(testRenderer: ReactTestRenderer, ...args: any[]):
| Error
| Promise<ReactTestRenderer[]>
| Promise<ReactTestRenderer>
| ReactTestRenderer[]
| ReactTestRenderer
| Promise<NativeTestInstance[]>
| Promise<NativeTestInstance>
| NativeTestInstance[]
| NativeTestInstance
| null;
}

Expand Down
10 changes: 5 additions & 5 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { getQueriesForElement, BoundFunction } from './get-queries-for-element';
declare const within: typeof getQueriesForElement;

interface Query extends Function {
(testRenderer: ReactTestRenderer | NativeTestInstance, ...args: any[]):
(testRenderer: ReactTestRenderer, ...args: any[]):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may be backwards. A query only takes a NativeTestInstance, never a ReactTestRenderer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I just thought testRenderer should be ReactTestRenderer by seeing these changes.

manakuro@d8c3b30#diff-0da64c26f363af99c40bbae0dc3fb0bfR8

Should it be replaced with NativeTestInstance in typings/queries.d.ts like this ?

export type QueryByBoundProp = (
  testRenderer: NativeTestInstance,
  id: Matcher,
  options?: MatcherOptions,
) => NativeTestInstance | null;
...

and Query just takes NativeTestInstance ?

interface Query extends Function {
  (testRenderer: NativeTestInstance, ...args: any[]):
    | Error
    | Promise<NativeTestInstance[]>
    | Promise<NativeTestInstance>
    | NativeTestInstance[]
    | NativeTestInstance
    | null;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is right. And to make it more clear, we might want to change the name of the argument from testRenderer as a part of this as well 👍

| Error
| Promise<HTMLElement[]>
| Promise<HTMLElement>
| HTMLElement[]
| HTMLElement
| Promise<NativeTestInstance[]>
| Promise<NativeTestInstance>
| NativeTestInstance[]
| NativeTestInstance
| null;
}

Expand Down
6 changes: 3 additions & 3 deletions typings/query-helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Matcher, MatcherOptions } from './matches';

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export interface SelectorMatcherOptions extends MatcherOptions {
export type SelectorMatcherOptions = Omit<MatcherOptions, 'selector'> & {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand what this one does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous one tried to extend MatcherOptions and override selector property but TypeScript cannot override interface property. So to do that, you can use Omit to exclude selector and extend it again.

https://stackoverflow.com/questions/41285211/overriding-interface-property-type-defined-in-typescript-d-ts-file?answertab=votes#tab-top

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks 👍

selector?: string;
}
};

type ReactTestInstance = {
getProp: (string) => NativeTestInstance;
getProp: (str: string) => NativeTestInstance;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this something more descriptive of what this is? Like name possibly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is just a syntax error. You need to use type and name together.

https://www.typescriptlang.org/docs/handbook/functions.html#writing-the-function-type

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, can we make the name of the argument name so that it’s clear from the typings what getProp is looking for? ☺️

};

export type NativeTestInstance = Omit<
Expand Down