in Education by
I am trying to declare a list which can either be full of type 'a' data or type 'b' at a time. I understand why typescript is giving an error here, it doesn't know inside the loop whether the user data member inside temp variable inside the loop will be of type 'a' or 'b'. What I don't understand is how to fix it. I want an array that can be either full of one or the other type of data at a time. error: Argument of type 'a | b' is not assignable to parameter of type 'b'. Property 'extra_porperty' is missing in type 'a' but required in type 'b'. interface a { id: number; first_name: string; last_name: string; } interface b extends a { extra_porperty: string; } interface c { user: a | b; } const newList: c[] = []; const tempList: a[] | b[] = []; newList.forEach((temp: c) => tempList.push(temp.user)); JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
What you did is assert that tempList is either an array of a or an array of b. That means the entire list must be all the same type. What you have to do is define tempList as an array of either one of those 2 types like this const tempList: (a | b)[] = []; That will allows elements of type a or b to be placed inside the array. Check out the working example in the playground here.

Related questions

0 votes
    I am trying to declare a list which can either be full of type 'a' data or type 'b' at a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    In the following program, if I set the variable $foo to the value 1 inside the first if statement, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    The following are ways to declare a variable in TypeScript, except ________. (1)var 2 (2)var localLength=13 (3)var lengthA:string = "meter" (4)var lengthB:string...
asked Aug 20, 2021 by JackTerrance
0 votes
    I have loop inside loop like this: var len = bolumlerUnique.length; function bolumleriGonder() { for (i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I have loop inside loop like this: var len = bolumlerUnique.length; function bolumleriGonder() { for (i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I have loop inside loop like this: var len = bolumlerUnique.length; function bolumleriGonder() { for (i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    It seems using getters inside templates causes Angular change detection to go into a loop (getter gets ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have this array of objects. let links = [ { url: 'some url 1', status: 200 }, { url: ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I'm trying to mock a TypeScript class with Jest and I'm obviously doing something because receive the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do should be something extremly simple but still I cannot seem to get it done ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    The pressure inside a piston cylinder is a variable of (a) Radius (b) Plane angle (c) ... theory proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Let's say I have the following simple table variable: declare @databases table ( DatabaseID int, Name ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I am developing Outlook add-ins which populates all the available shared mailboxes in to combo box and sends ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...