panaverse / learn-typescript

Learning TypeScript in Baby Steps
MIT License
1.28k stars 548 forks source link

Why am I seeing console.log output for non-exported object properties in my TypeScript code and how can I prevent this? #13

Open noman-maken opened 1 year ago

noman-maken commented 1 year ago

I have two files, index.ts and app.ts. The index.ts file defines two objects,profile and person. profile is not exported, but it is logged to the console using console.log(profile["age"])and console.log(profile["education@"]). person is exported as the default export.

In app.ts, I import person using import person from "./index.js"; and log person.name to the console using console.log(person.name);. I compile the TypeScript code using tsc and then run the resulting JavaScript code using node app.js.

index.ts

const profile = {
    name : "Noman..",
    age : 25,
    "education@": "BSSEs",
}

const person = {
    name : "Noman",
    age : 24,
    "education@": "BSSE",
}

console.log(profile["age"]);
console.log(profile["education@"]);

export default person;

app.ts

import person from "./index.js";

console.log(person.name);

The output is

25
BSSEs
Noman

My question is, why am I seeing console.log output for profile properties in my app.js output even though I have not exported the profile object from index.ts? Also, how can I prevent this console output from appearing in my app.jsoutput?

fahad987 commented 1 year ago

The reason why you are seeing console.log output for the properties of the profile object in your app.js output is that the profile object is defined in index.ts and is executed when you run your app.js file. Since the console.log statements for the profile object are present in index.ts, they will be executed and logged to the console when you run app.js. To prevent the console output of profile properties from appearing in your app.js output, you can simply remove or comment out the console.log statements that reference the profile object in your index.ts file. This way, the code in your app.ts file will only log the properties of the exported person object, and not the profile object. On Sunday, April 9, 2023 at 02:18:29 PM GMT+5, Muhammad Noman Aslam Maken @.***> wrote:

I have two files, index.ts and app.ts. The index.ts file defines two objects, profile and person. profile is not exported, but it is logged to the console using console.log(profile["age"]) and console.log(profile["education@"]). person is exported as the default export.

In app.ts, I import person using import person from "./index.js"; and log person.name to the console using console.log(person.name);. I compile the TypeScript code using tsc and then run the resulting JavaScript code using node app.js.

index.ts const profile = { name : "Noman..", age : 25, "education@": "BSSEs", }

const person = { name : "Noman", age : 24, "education@": "BSSE", }

console.log(profile["age"]); console.log(profile["education@"]);

export default person;

app.ts import person from "./index.js";

console.log(person.name);

The output is 25 BSSEs Noman

My question is, why am I seeing console.log output for profile properties in my app.js output even though I have not exported the profile object from index.ts? Also, how can I prevent this console output from appearing in my app.js output?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>