Error: Gulp is using ts-node and returning 'void' instead of 'Task', but it cannot find the type 'Task'

Seeking assistance from experienced individuals in the realm of gulp.js and typescript - could someone provide guidance for a struggling newcomer?

I am currently utilizing the most recent versions of all relevant tools (node, ts-node, gulp, ts, @types/gulp, etc). Gulp is being executed through ts-node, using a gulpfile.ts instead of gulpfile.js, but I am encountering the following error:

error TS2345: Argument of type 'Promise<void>' is not assignable to parameter of type 'Task'.
Type 'Promise<void>' does not match the signature expected by 'TaskFunction'.
The return type ''Promise<void>'' is incompatible with '(done: TaskCallback): void | EventEmitter | ChildProcess | Stream | Observable<any> | PromiseLike<any>'.

The key portions of my code are as follows:

gulp.parallel(
    subTask("directory_1"),
    subTask("directory_2")
);

async function subTask(sDirectory:string){
    await gulp.src("src/"+sDirectory+"/*.*")
        // Perform tasks such as linting, transpiling, uglifying, etc.
        .pipe(gulp.dest("dest/"+sDirectory+"/"));
    return;
}

The code functions correctly when the directory strings are hardcoded within subTask (creating separate copies for each directory), however it fails when attempting to pass the directories as parameters.

It appears that the issue lies in returning a void promise from subTask whereas gulp expects a type of Task (a subtype of

TaskFunction</code). Trouble arises when trying to assign a type to <code>subTask
(assigning either Task or TaskFunction results in "Cannot find name 'Task'").

What mistake am I making here? And more importantly, how can I resolve it? Can anyone provide example code to rectify this issue?

Your assistance and guidance would be immensely appreciated. Thank you.

Best regards,

Dulux-Oz

Answer №1

Your function has two main issues: firstly, it doesn't return anything; and secondly, it is marked as async. While gulp streams operate in an asynchronous manner, they are not promises, so there is no need to await them. Therefore, the async declaration is unnecessary.

To address this, I recommend updating your function as follows:

function subTask(sDirectory:string) {
    return function() {
        return gulp.src("src/"+sDirectory+"/*.*")
        // Perform linting, transpiling, uglifying, etc
        .pipe(gulp.dest("dest/"+sDirectory+"/"));
    };
}

The remaining portion of your code appears to be correct.

Answer №2

After receiving advice from @goto-0, I found that their answer was 99% accurate (big thanks to @goto-0). However, following the suggested refactor, I encountered an error stating

Did you forget to signal async completion?
.

The complete solution required making the sub-function inside subTask an asynchronous function:

function subTask(sDirectory:string) {
    return async function() {
        return gulp.src("src/"+sDirectory+"/*.*")
        // Further processing like linting, transpiling, uglifying, etc
        .pipe(gulp.dest("dest/"+sDirectory+"/"));
    };
}

All is well now :-)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When incorporating a second overload as an object, it is generating a pair of errors

I'm currently working on creating an overload function that takes either two arguments or one argument as an object, which will be used in the following way: // Two parameters obj.set('a', '123'); obj.set('b', 'efg&a ...

The operation failed with a TypeError because the object does not allow the addition of the newField property

I encountered an error that says: TypeError: Cannot add property newField, object is not extensible Whenever I try to add a new key or change a value, it doesn't work and I'm not sure what the issue is. dynamicFilter; public ionViewWillEnte ...

What is the best way to transfer user data from the backend to the frontend?

I successfully created a replica of YelpCamp using Node and EJS, but now I am attempting to convert it into a Node-React project. Everything was going smoothly until I encountered an issue while trying to list a specific user in the SHOW route. In order to ...

Setting up Firebase App Check in an Angular projectWould you like to know

I have a question about initializing Firebase app check with Angular. I am currently using AngularFire, but I'm not sure how to initialize Firebase app check before using any services. The documentation provides the following initialization code to b ...

Assign a property to an array of objects depending on the presence of a value in a separate array

Looking to manipulate arrays? Here's a task for you: const arrayToCheck = ['a', 'b', 'c', 'd']; We have the main array as follows: const mainArray = [ {name:'alex', code: 'c'}, ...

Creating a personal TypeScript language service extension in Visual Studio Code

Currently, I am working on developing a TSserver plugin in VSCode and struggling to get the server to load my plugin. I have experimented with setting the path in tsconfig.json to both a local path and a path to node_modules, but it still isn't worki ...

The act of exporting an enum from a user-defined TypeScript path leads to the error message "Module not

I have set up a custom path as explained in this particular discussion. "baseUrl": ".", "paths": { "@library/*": [ "./src/myFolder/*" ], } Within this module, I am exporting an Enum. export enum EN ...

efficiently managing errors in a Nest Jest microservice with RabbitMQ

https://i.sstatic.net/sUGm1.png There seems to be an issue with this microservice, If I throw an exception in the users Service, it should be returned back to the gateway and then to the client However, this is not happening! The client only sees the de ...

Having an issue with forkJoin where the code seems to get stuck and does not continue execution after

The following script is retrieving two values from the database. I am using forkJoin for this purpose, which is a new approach for me. The reason behind utilizing this method is that there is a specific function that requires both values to be fetched bef ...

Nuxt 3 turns a blind eye to TypeScript errors upon code saving

I am facing an issue. Even with a TypeScript error in the code, the IDE shows an error, but I am still able to save it and run it in the browser. Is this acceptable? Check out the code below: <script lang="ts" setup> const a = ref<strin ...

After successfully building with Vite, an error occurs stating "TypeError: can't convert undefined to object." However, during development with Vite, everything functions flawlessly

Currently, I am utilizing Vite in conjunction with React and Typescript for my project. Interestingly, when I execute 'vite dev', the live version of the website works flawlessly without any errors showing up on the console. However, things take ...

What is the recommended way to use the async pipe to consume a single Observable property in a view from an Angular2

Suppose I have a component with the following property: import { Component, OnInit } from 'angular2/core'; import { CarService } from 'someservice'; @Component({ selector: 'car-detail', templateUrl: './app/cars/ ...

Tips on inferring a distinct generic type for every element within an array of objects

I'm working on code that creates a timeline chart for data. I want the code to accept an array of series, each containing data and various getters used to position and render the data on the chart. Below is a simplified example of the code. The actua ...

What is the correct way to declare a variable with a generic type parameter?

Exploring the following code snippet that showcases a React component being defined with a type argument named TRow: function DataTable<TRow> ({ rows: TRow[] }) { return ( ) } Prior to this implementation, ES6 was utilized and components were c ...

Create a system for detecting changes in simple input elements and triggering a function to update the final result

There are a maximum of 12 inputs that represent the same entities or objects but with varying integer values. These values directly impact the final result displayed to the user. Whenever any of the input values change, a function needs to be triggered to ...

When utilizing the useRef hook in Material-UI, an error may arise stating: "The property 'value' is not found on the type 'never'."

Currently, I am utilizing material UI to construct a login and registration page. In the process, I am leveraging the useRef hook to retrieve a reference instance for a TextFiled, and using xxxRef.current.value to access the input value. Despite being abl ...

Guide on transforming a JSON string into an array of custom objects using the json2typescript NPM module within a TypeScript environment

I am looking to utilize the json2typescript NPM module to convert a JSON string into an array of custom objects. Below is the code I have written. export class CustomObject { constructor(private property1: string, private property2: string, private p ...

How can one determine the logical data type within a cell array of strings in Matlab?

Consider the following scenario, where we have a cell array of strings: C = {'a' '1'; 'b' '2015-08-04'} What I am looking for is a function called getCellType that takes in a cell array as input and returns a cell ...

Arranging React Grid Items with Stylish Overlapping Layout

Is there a way to create a react-grid-layout with 100 grid points width, while ensuring that the grid items do not overlap? (Reducing the number of columns can prevent overlap, but sacrifices the 100-point width resolution for grid placement) import Butto ...

What is the syntax for defining parameters in an overloaded arrow function in TypeScript?

Trying to create an async arrow function that can handle a single image object or an array of image objects. I'm new to TypeScript overloading and may be approaching this the wrong way. Here's what I've come up with: type ImageType = { ...