Skip to content
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
11 changes: 11 additions & 0 deletions types/prop-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ export interface Requireable<T> extends Validator<T | undefined | null> {

export type ValidationMap<T> = { [K in keyof T]?: Validator<T[K]> };

/**
* Like {@link ValidationMap} but treats `undefined`, `null` and optional properties the same.
* This type is only added as a migration path in React 19 where this type was removed from React.
* Runtime and compile time types would mismatch since you could see `undefined` at runtime when your types don't expect this type.
*/
export type WeakValidationMap<T> = {
[K in keyof T]?: null extends T[K] ? Validator<T[K] | null | undefined>
: undefined extends T[K] ? Validator<T[K] | null | undefined>
: Validator<T[K]>;
};

export type InferType<V> = V extends Validator<infer T> ? T : any;
export type InferProps<V> =
& InferPropsInner<Pick<V, RequiredKeys<V>>>
Expand Down
11 changes: 11 additions & 0 deletions types/prop-types/prop-types-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ const propTypes: PropTypesMap = {
component: PropTypes.elementType.isRequired,
};

const strongIncorrectPropTypes: PropTypes.ValidationMap<{ foo: number | null }> = {
// @ts-expect-error
foo: PropTypes.number,
};
const strongCorrectPropTypes: PropTypes.ValidationMap<{ foo: number | null | undefined }> = {
foo: PropTypes.number,
};
const weakPropTypes: PropTypes.WeakValidationMap<{ foo: number | null }> = {
foo: PropTypes.number,
};

// JS checking
const propTypesWithoutAnnotation = {
any: PropTypes.any,
Expand Down
15 changes: 15 additions & 0 deletions types/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4069,18 +4069,33 @@ declare namespace React {
// React.PropTypes
// ----------------------------------------------------------------------

/**
* @deprecated Use `Validator` from the ´prop-types` instead.
*/
type Validator<T> = PropTypes.Validator<T>;

/**
* @deprecated Use `Requireable` from the ´prop-types` instead.
*/
type Requireable<T> = PropTypes.Requireable<T>;

/**
* @deprecated Use `ValidationMap` from the ´prop-types` instead.
*/
type ValidationMap<T> = PropTypes.ValidationMap<T>;

/**
* @deprecated Use `WeakValidationMap` from the ´prop-types` instead.
*/
type WeakValidationMap<T> = {
[K in keyof T]?: null extends T[K] ? Validator<T[K] | null | undefined>
: undefined extends T[K] ? Validator<T[K] | null | undefined>
: Validator<T[K]>;
};

/**
* @deprecated Use `PropTypes.*` where `PropTypes` comes from `import * as PropTypes from 'prop-types'` instead.
*/
interface ReactPropTypes {
any: typeof PropTypes.any;
array: typeof PropTypes.array;
Expand Down
15 changes: 15 additions & 0 deletions types/react/ts5.0/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4070,18 +4070,33 @@ declare namespace React {
// React.PropTypes
// ----------------------------------------------------------------------

/**
* @deprecated Use `Validator` from the ´prop-types` instead.
*/
type Validator<T> = PropTypes.Validator<T>;

/**
* @deprecated Use `Requireable` from the ´prop-types` instead.
*/
type Requireable<T> = PropTypes.Requireable<T>;

/**
* @deprecated Use `ValidationMap` from the ´prop-types` instead.
*/
type ValidationMap<T> = PropTypes.ValidationMap<T>;

/**
* @deprecated Use `WeakValidationMap` from the ´prop-types` instead.
*/
type WeakValidationMap<T> = {
[K in keyof T]?: null extends T[K] ? Validator<T[K] | null | undefined>
: undefined extends T[K] ? Validator<T[K] | null | undefined>
: Validator<T[K]>;
};

/**
* @deprecated Use `PropTypes.*` where `PropTypes` comes from `import * as PropTypes from 'prop-types'` instead.
*/
interface ReactPropTypes {
any: typeof PropTypes.any;
array: typeof PropTypes.array;
Expand Down