Skip to content

Commit 76223e7

Browse files
authored
TypeScript: Migrate deprecated package to TypeScript (#70362)
* refactor: migrate deprecated package from js to ts for type safety * docs: move inline param docs to type definition * docs: add the missing message param inline comment * docs: fix param documentation for deprecated action
1 parent 8ae94de commit 76223e7

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

packages/deprecated/README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,15 @@ deprecated( 'Eating meat', {
5959
_Parameters_
6060

6161
- _feature_ `string`: Name of the deprecated feature.
62-
- _options_ `[Object]`: Personalisation options
63-
- _options.since_ `[string]`: Version in which the feature was deprecated.
64-
- _options.version_ `[string]`: Version in which the feature will be removed.
65-
- _options.alternative_ `[string]`: Feature to use instead
66-
- _options.plugin_ `[string]`: Plugin name if it's a plugin feature
67-
- _options.link_ `[string]`: Link to documentation
68-
- _options.hint_ `[string]`: Additional message to help transition away from the deprecated feature.
62+
- _options_ `[DeprecatedOptions]`: Personalisation options
6963

7064
### logged
7165

7266
Object map tracking messages which have been logged, for use in ensuring a message is only logged once.
7367

7468
_Type_
7569

76-
- `Record<string, true | undefined>`
70+
- `Record< string, true >`
7771

7872
<!-- END TOKEN(Autogenerated API docs) -->
7973

packages/deprecated/src/index.js renamed to packages/deprecated/src/index.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,41 @@ import { doAction } from '@wordpress/hooks';
66
/**
77
* Object map tracking messages which have been logged, for use in ensuring a
88
* message is only logged once.
9-
*
10-
* @type {Record<string, true | undefined>}
119
*/
12-
export const logged = Object.create( null );
10+
export const logged: Record< string, true > = Object.create( null );
11+
12+
type DeprecatedOptions = {
13+
/**
14+
* Version in which the feature was deprecated.
15+
*/
16+
since?: string;
17+
/**
18+
* Version in which the feature will be removed.
19+
*/
20+
version?: string;
21+
/**
22+
* Feature to use instead.
23+
*/
24+
alternative?: string;
25+
/**
26+
* Plugin name if it's a plugin feature.
27+
*/
28+
plugin?: string;
29+
/**
30+
* Link to documentation.
31+
*/
32+
link?: string;
33+
/**
34+
* Additional message to help transition away from the deprecated feature.
35+
*/
36+
hint?: string;
37+
};
1338

1439
/**
1540
* Logs a message to notify developers about a deprecated feature.
1641
*
17-
* @param {string} feature Name of the deprecated feature.
18-
* @param {Object} [options] Personalisation options
19-
* @param {string} [options.since] Version in which the feature was deprecated.
20-
* @param {string} [options.version] Version in which the feature will be removed.
21-
* @param {string} [options.alternative] Feature to use instead
22-
* @param {string} [options.plugin] Plugin name if it's a plugin feature
23-
* @param {string} [options.link] Link to documentation
24-
* @param {string} [options.hint] Additional message to help transition away from the deprecated feature.
42+
* @param {string} feature Name of the deprecated feature.
43+
* @param {DeprecatedOptions} [options] Personalisation options
2544
*
2645
* @example
2746
* ```js
@@ -38,7 +57,10 @@ export const logged = Object.create( null );
3857
* // Logs: 'Eating meat is deprecated since version 2019.01.01 and will be removed from the earth in version 2020.01.01. Please use vegetables instead. Note: You may find it beneficial to transition gradually.'
3958
* ```
4059
*/
41-
export default function deprecated( feature, options = {} ) {
60+
export default function deprecated(
61+
feature: string,
62+
options: DeprecatedOptions = {}
63+
) {
4264
const { since, version, alternative, plugin, link, hint } = options;
4365

4466
const pluginMessage = plugin ? ` from ${ plugin }` : '';
@@ -61,15 +83,9 @@ export default function deprecated( feature, options = {} ) {
6183
/**
6284
* Fires whenever a deprecated feature is encountered
6385
*
64-
* @param {string} feature Name of the deprecated feature.
65-
* @param {?Object} options Personalisation options
66-
* @param {string} options.since Version in which the feature was deprecated.
67-
* @param {?string} options.version Version in which the feature will be removed.
68-
* @param {?string} options.alternative Feature to use instead
69-
* @param {?string} options.plugin Plugin name if it's a plugin feature
70-
* @param {?string} options.link Link to documentation
71-
* @param {?string} options.hint Additional message to help transition away from the deprecated feature.
72-
* @param {?string} message Message sent to console.warn
86+
* @param {string} feature Name of the deprecated feature.
87+
* @param {DeprecatedOptions} options Personalisation options
88+
* @param {string} message Message sent to console.warn
7389
*/
7490
doAction( 'deprecated', feature, options, message );
7591

@@ -78,5 +94,3 @@ export default function deprecated( feature, options = {} ) {
7894

7995
logged[ message ] = true;
8096
}
81-
82-
/** @typedef {import('utility-types').NonUndefined<Parameters<typeof deprecated>[1]>} DeprecatedOptions */

packages/deprecated/src/test/index.js renamed to packages/deprecated/src/test/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { didAction } from '@wordpress/hooks';
66
/**
77
* Internal dependencies
88
*/
9-
import deprecated, { logged } from '../';
9+
import deprecated, { logged } from '..';
1010

1111
describe( 'deprecated', () => {
1212
afterEach( () => {

0 commit comments

Comments
 (0)