1

Here i have classes file

export class Emp{
EmpId:number;
EmpName:String,
EmpSal:string
}

How can i wrote static object Like EmpId=1,EmpNamw=john,Empsal=200 EmpId=2,EmpName=smith,EmpSal=300

1

2 Answers 2

5

Typescript doesn't work like that. Its module based. You should export a let or const

in your case it would be

export let Emp = { 
    EmpId: 2,
    EmpName: 'Bla',
    EmpSal: 'Bla',
}
Sign up to request clarification or add additional context in comments.

1 Comment

Dude. Why is Typescript like this?
2

I used an interface to create a list of Emp objects and JSON.stringify to get them as a JSON string (you can see the result in the jsfiddle).

I guess you try to do something like this:

interface IEmp{
    EmpId:number;
    EmpName:string;
    EmpSal:string;
}

let emp1 : IEmp = {EmpId:1, EmpName:'name1', EmpSal:'sal1'};
let emp2 : IEmp = {EmpId:2, EmpName:'name2', EmpSal:'sal2'};

let lstEmp : Array<IEmp> = [emp1, emp2];

document.body.innerHTML = JSON.stringify(lstEmp);

https://jsfiddle.net/kkxw1y0k/

3 Comments

Please explain what you are doing in this code, don't just paste code.
I used an interface to create a list of Emp objects and JSON.stringify to get them as a JSON string (you can see the result in the jsfiddle)
Thanks for the explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.