Types when destructuring arrays

后端 未结 5 1790
感动是毒
感动是毒 2020-12-14 05:21
function f([a,b,c]) {
  // this works but a,b and c are any
}

it\'s possible write something like that?

function f([a: number,b: nu         


        
5条回答
  •  我在风中等你
    2020-12-14 05:44

    my code was something like below

    type Node = { 
        start: string;
        end: string;
        level: number;
    };
    
    const getNodesAndCounts = () => { 
        const nodes : Node[]; 
        const counts: number[];
        // ... code here
    
    return [nodes, counts];
    }
    
    const [nodes, counts] = getNodesAndCounts(); // problematic line needed type
    

    typescript was giving me error in line below TS2349: Cannot invoke an expression whose type lacks a call signature;

    nodes.map(x => { 
    //some mapping; 
        return x;
    );
    

    Changing line to below resolved my problem;

    const [nodes, counts] = getNodesAndCounts();
    

提交回复
热议问题