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

incorrect Property '...' for select/dispatch in @wordpress/data 8.4.x with Typescript #48133

Closed
Chrico opened this issue Feb 16, 2023 · 1 comment · Fixed by #48221
Closed
Labels
[Package] Data /packages/data [Type] Bug An existing feature does not function as intended

Comments

@Chrico
Copy link
Contributor

Chrico commented Feb 16, 2023

Description

The newest @wordpress/data version 8.4.0 does not work with Typescript.

This error does not occur when switching to 8.3.x in package.json. It seems that the select() (and also dispatch()) function do not have the correct Typescript types / return types / docblock types and therfor Typescript parsing fails. It actually already fails in the IDE (PHPStorm) due "underlining" the code with notice: TS2339: Property 'getCurrentPostType' does not exist on type 'Object'.

When building assets it fails due following errors (my example code - see below):

> yarn build
yarn run v1.22.15
warning package.json: No license field
$ wp-scripts build
assets by status 396 bytes [cached] 2 assets
Entrypoint test = test.js test.asset.php
./test.ts 258 bytes [built] [code generated] [2 errors]
external ["wp","data"] 42 bytes [built] [code generated]

ERROR in .\wordpress-data-8.4\test.ts
./test.ts 4:37-55
[tsl] ERROR in .\wordpress-data-8.4\test.ts(4,38)
      TS2339: Property 'getCurrentPostType' does not exist on type 'Object'.
ts-loader-default_e3b0c44298fc1c14

ERROR in .\wordpress-data-8.4\test.ts
./test.ts 5:48-57
[tsl] ERROR in .\wordpress-data-8.4\test.ts(5,49)
      TS2339: Property 'getBlocks' does not exist on type 'Object'.
ts-loader-default_e3b0c44298fc1c14

ERROR in .\test-wordpress-data-8.4\test.ts
./test.ts 6:32-43
[tsl] ERROR in .\test-wordpress-data-8.4\test.ts(6,33)
      TS2339: Property 'resetBlocks' does not exist on type 'Object'.

webpack 5.75.0 compiled with 2 errors in 3117 ms
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Step-by-step reproduction instructions

1. Create a new folder and insert following package.json:

{
    "name": "wordpress-data-8.4",
    "version": "1.0.0",
    "devDependencies": {
        "@wordpress/scripts": "25.4.0",
        "ts-loader": "9.4.2",
        "typescript": "4.9.5"
    },
    "dependencies": {
        "@wordpress/data": "8.4.0"
    },
    "scripts": {
        "build": "wp-scripts build",
        "lint:js": "wp-scripts lint-js *.ts"
    }
}

2. Create a tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "removeComments": true,
        "lib": [
            "dom",
            "dom.iterable",
            "es5",
            "es6",
            "es7",
            "es2017"
        ],
        "resolveJsonModule": true,
        "importHelpers": true,
        "moduleResolution": "node",
        "declaration": false,
        "esModuleInterop": true,
        "noImplicitAny": false,
        "jsx": "react",
        "jsxFactory": "wp.element.createElement",
        "sourceMap": true,
        "noLib": false,
        "suppressImplicitAnyIndexErrors": true,
        "downlevelIteration": true
    },
    "compileOnSave": false,
    "exclude": [
        "node_modules",
        "vendor"
    ]
}

3. Create a webpack.config.js:

const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );

const config = {
    ...defaultConfig,
    module: {
        ...defaultConfig.module,
        rules: [
            ...defaultConfig.module.rules,
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
};

module.exports = {
    ...config,
    entry: {
        'test': './test'
    },
    output: {
        path: __dirname + '/assets',
        filename: '[name].js',
    },
};

4. Create a test.ts

import { select } from '@wordpress/data';

const postType = select( 'core/data' ).getCurrentPostType();
const blockList = select( 'core/block-editor' ).getBlocks();

dispatch( 'core/block-editor' ).resetBlocks( { } );

5. run yarn build

It fails - see error above.

Screenshots, screen recording, code snippet

No response

Environment info

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@shvlv
Copy link
Contributor

shvlv commented Feb 16, 2023

It seems it's caused by #46881

@kathrynwp kathrynwp added [Package] Data /packages/data [Type] Bug An existing feature does not function as intended labels Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Data /packages/data [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants