Home > Enterprise >  How do I change this function signature to take an object instead of a series of simple args to impr
How do I change this function signature to take an object instead of a series of simple args to impr

Time:10-12

Given the below code, I'd like to change it to accept an object for improved readability, but I keep getting ide errors when I do so.

declare global {
    // eslint-disable-next-line @typescript-eslint/no-namespace
    namespace Cypress {
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        interface Chainable<Subject> {
            doStuff: (
                paramA?: string,
                paramB?: string,
                paramC?: boolean
            ) => Chainable<Element>;
            doMoreStuff: () => Chainable<Element>;
        }
    }
}

export const doStuffCommand: NxCypressCommandDefinition = {
    name: 'doStuff',
    command: (
        paramA: string = var1,
        paramB: string = var2,
        paramC = true
    ) => {
        if (paramC) {
            performTask();
        }
        cy.interceptGraphql();
        cy.get('selector').type(paramA);
        cy.get('selector').type(paramB);
        cy.get('selector').click();
    }
};

Ideally I'd like paramA, paramB & paramC to be in an object, I have tried the code below but it doesn't seem to work:

declare global {
    // eslint-disable-next-line @typescript-eslint/no-namespace
    namespace Cypress {
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        interface Chainable<Subject> {
            doStuff: ({
                paramA?: string,
                paramB?: string,
                paramC?: boolean
            }) => Chainable<Element>;
            doMoreStuff: () => Chainable<Element>;
        }
    }
}

export const doStuffCommand: NxCypressCommandDefinition = {
    name: 'doStuff',
    command: ({
        paramA: string = var1,
        paramB: string = var2,
        paramC = true
    }) => {
        if (paramC) {
            performTask();
        }
        cy.get('selector').type(paramA);
        cy.get('selector').type(paramB);
        cy.get('selector').click();
    }
};

CodePudding user response:

This gets quite a lot of people. The type should be after the destructuring:

command: ({
  paramA = var1,
  paramB = var2,
  paramC = true,
}: {
  paramA?: string;
  paramB?: string;
  paramC?: boolean;
}) => {

As for your type definition, you need a parameter name:

doStuff: (params: {
//        ~~~~~~
  paramA?: string,
  paramB?: string,
  paramC?: boolean
}) => Chainable<Element>;
  • Related