Typemart project bug - Typescript

In task 5, I did this and got an error in the terminal after running tsc.

if (product.preOrder) {
  console.log("We'll notify you when your item has been dispatched")
}

It says ‘Object is possibly undefined’ but I am absolutely sure everything is correct, and even looks identical to the walkthrough instructor’s code and his was fine. Bug or no?

1 Like

I realised right before finishing it that I was using .find(), which could potentially return ‘undefined’ if no element is found. I replaced it for .filter() and worked fine.

2 Likes

I think it’s really not great that the course designer(s) let beginners run into this pitfall by proposing .find in the hint without giving some explanation about the implication.

IMO a guided course should provide th is help, so that I don’t need to grind through it myself. This is worsening the user experience a lot for me.

@codecademy, please consider fixing this.

1 Like

hey everyone as I am learning typemart. I face an issue in Typemart Project.
I am unable to import the array present in different file.
After writing this line “import products from ‘./products’;” in index.ts file, when I run the file. I got import error.
///////////////////////////////////////
$ node index.ts
(node:120) Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)
/home/ccuser/workspace/typescript-type-mart-V2/index.ts:1
import products from ‘./products’;
^^^^^^

SyntaxError: Cannot use import statement outside a module
//////////////////////////////////
anyone who can help what’s wrong in this.

In the terminal, run tsc index.ts to compile the index.js file

Then run node index.js

I hope this helps

I am getting errors with this project too.
How are we supposed to convert it to Boolean?

Code

import products from './products';

const productName: string = 'fanny pack';
const product = products.filter(product => product.name === productName)[0];

console.log(product)

if (product.preOrder === true) {
  console.log('We will send you a message when your product is on its way.');
}

Error

index.ts:8:5 - error TS2367: This condition will always return ‘false’ since the types ‘string’ and ‘boolean’ have no overlap.

8 if (product.preOrder === true) {

You haven’t shared what products looks like, so we can’t see the data structure and the data types involved.

I am assuming that the preOrder property value has been assigned the string values 'true' and 'false' (the error message hints at this) instead of the boolean true and false. You should look at the products file (that you are importing from) to verify the actual situation.

If the above assumption is correct, then there are multiple ways you can handle it.

One way would be to amend your condition to:

if (product.preOrder === 'true') {

to avoid the string and boolean mismatch. Now the if condition is comparing two strings.

Alternatively, you could convert the string to boolean.

Something like:

product.preOrder = (product.preOrder === 'true');
// This will return a boolean value, but it will modify your original data.
// And then your condition could be:
// if (product.preOrder === true) {
// Now, the if condition is comparing two booleans.

// If you don't want to modify the original data, then assign the result 
// to another variable.
let newVariable = (product.preOrder === 'true');
// where you should pick a more readable and more appropriate variable 
// name instead of newVariable. 
// And then your condition could be:
// if (newVariable === true) {
// Now, the if condition is comparing two booleans.

First of all, go to the products file and see what values have been assigned to the preOrder property. Once you understand the data structure and types involved, then there are multiple approaches you can take to write your conditions.