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

Add some utility generics for better typings #8815

Merged
merged 2 commits into from
Apr 7, 2023
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
6 changes: 3 additions & 3 deletions packages/ra-core/src/core/ResourceDefinitionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as React from 'react';
import { createContext, useCallback, useState, useMemo } from 'react';
import isEqual from 'lodash/isEqual';

import { ResourceDefinition } from '../types';
import { ResourceDefinition, ResourceOptions } from '../types';

export type ResourceDefinitions = {
[name: string]: ResourceDefinition;
export type ResourceDefinitions<OptionsType extends ResourceOptions = any> = {
[name: string]: ResourceDefinition<OptionsType>;
};

export type ResourceDefinitionContextValue = {
Expand Down
8 changes: 5 additions & 3 deletions packages/ra-core/src/core/useResourceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import defaults from 'lodash/defaults';

import { useResourceDefinitions } from './useResourceDefinitions';
import { useResourceContext } from './useResourceContext';
import { ResourceDefinition } from '../types';
import { ResourceDefinition, ResourceOptions } from '../types';

/**
* Hook to get the definition of a given resource
Expand All @@ -26,9 +26,11 @@ import { ResourceDefinition } from '../types';
*
* const definition = useResourceDefinition({ resource: 'posts' });
*/
export const useResourceDefinition = (
export const useResourceDefinition = <
OptionsType extends ResourceOptions = any
>(
props?: UseResourceDefinitionOptions
): ResourceDefinition => {
): ResourceDefinition<OptionsType> => {
const resource = useResourceContext(props);
const resourceDefinitions = useResourceDefinitions();
const { hasCreate, hasEdit, hasList, hasShow, recordRepresentation } =
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-core/src/core/useResourceDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ import { useResourceDefinitionContext } from './useResourceDefinitionContext';
* // recordRepresentation: 'title',
* // }
*/
export const useResourceDefinitions = (): ResourceDefinitions =>
export const useResourceDefinitions = <
OptionsType = any
>(): ResourceDefinitions<OptionsType> =>
useResourceDefinitionContext().definitions;
4 changes: 3 additions & 1 deletion packages/ra-core/src/form/useInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const defaultFormat = (value: any) => (value == null ? '' : value);
// parse empty string into null as it's more suitable for a majority of backends
const defaultParse = (value: string) => (value === '' ? null : value);

export const useInput = (props: InputProps): UseInputValue => {
export const useInput = <ValueType = any>(
props: InputProps<ValueType>
): UseInputValue => {
const {
defaultValue,
format = defaultFormat,
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ export type LegacyDataProvider = (

export type RecordToStringFunction = (record: any) => string;

export interface ResourceDefinition {
export interface ResourceDefinition<OptionsType extends ResourceOptions = any> {
readonly name: string;
readonly options?: any;
readonly options?: OptionsType;
readonly hasList?: boolean;
readonly hasEdit?: boolean;
readonly hasShow?: boolean;
Expand Down