Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS-basics was converted to TS_basics. #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

JS-basics was converted to TS_basics. #15

wants to merge 2 commits into from

Conversation

MahtaMirzaei
Copy link
Contributor

closes: #14

@@ -0,0 +1,73 @@
function add(a: number, b: number): number {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you run the ts code?
I don't find the tsconfig.json.
Please configure your TypeScript.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use https://typestrong.org/ts-node/docs/ to run ts code, also you can use npx tsc [your file]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The self "TypeScript" infers the output type.
This means that when two numbers are added together, the Typescript understands that the output is a number and it's unnecessary to explicitly declare what the output is

@@ -0,0 +1,73 @@
function add(a: number, b: number): number {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function add(a: number, b: number): number {
function add(a: number, b: number) {

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

structure file should be mahta/TS/basic.ts

return a + b;
}

function flipACoin(): string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

function flipACoin(): string {
const random: number = Math.random();
if (random > 0.5) {
return "tail";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "tail";
return "tail" as const;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

function concat(stringA: string, stringB: string): string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here ...
fix all output types

console.log(`Hi ${name}, nice to meet you!`);
}

function getNRandomNumbers(n: number, min: number = 0, max: number = 1000): number[] {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you set default it doesn't need to define the type

console.log(`Hi ${name}, nice to meet you!`);
}

function getNRandomNumbers(n: number, min: number = 0, max: number = 1000): number[] {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function getNRandomNumbers(n: number, min: number = 0, max: number = 1000): number[] {
function getNRandomNumbers(n: number, min = 0, max= 1000): number[] {

}
}

function isNil(param: any): boolean {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not about ts:) This function is not implemented correctly. isNil returns true only when the input is undefined or null. However, for your function, if we pass 0 or a false value, the output is true.

return !param;
}

function callPropInObj(object: any, propertyName: string): void {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object is not a good name, don't use js keyword as a variable name!

Copy link

@minasdq minasdq Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the object type any?

return !param;
}

function callPropInObj(object: any, propertyName: string): void {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function callPropInObj(object: any, propertyName: string): void {
function callPropInObj(obj: object, propertyName: string) {

let fruits: string[] = ['Apple', 'Orange', 'Strawberry'];
fruits = fruits.concat(fruits);
return fruits;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MahtaMirzaei MahtaMirzaei requested a review from minasdq March 30, 2024 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TS basics
2 participants