-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
mahta/TS/TS basics/basics.ts
Outdated
@@ -0,0 +1,73 @@ | |||
function add(a: number, b: number): number { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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
mahta/TS/TS basics/basics.ts
Outdated
@@ -0,0 +1,73 @@ | |||
function add(a: number, b: number): number { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function add(a: number, b: number): number { | |
function add(a: number, b: number) { |
There was a problem hiding this comment.
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
mahta/TS/TS basics/basics.ts
Outdated
return a + b; | ||
} | ||
|
||
function flipACoin(): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
mahta/TS/TS basics/basics.ts
Outdated
function flipACoin(): string { | ||
const random: number = Math.random(); | ||
if (random > 0.5) { | ||
return "tail"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return "tail"; | |
return "tail" as const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please read this article about as const
https://www.totaltypescript.com/concepts/as-const
https://stackoverflow.com/questions/66993264/what-does-the-as-const-mean-in-typescript-and-what-is-its-use-case
mahta/TS/TS basics/basics.ts
Outdated
} | ||
} | ||
|
||
function concat(stringA: string, stringB: string): string { |
There was a problem hiding this comment.
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
mahta/TS/TS basics/basics.ts
Outdated
console.log(`Hi ${name}, nice to meet you!`); | ||
} | ||
|
||
function getNRandomNumbers(n: number, min: number = 0, max: number = 1000): number[] { |
There was a problem hiding this comment.
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
mahta/TS/TS basics/basics.ts
Outdated
console.log(`Hi ${name}, nice to meet you!`); | ||
} | ||
|
||
function getNRandomNumbers(n: number, min: number = 0, max: number = 1000): number[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function getNRandomNumbers(n: number, min: number = 0, max: number = 1000): number[] { | |
function getNRandomNumbers(n: number, min = 0, max= 1000): number[] { |
mahta/TS/TS basics/basics.ts
Outdated
} | ||
} | ||
|
||
function isNil(param: any): boolean { |
There was a problem hiding this comment.
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.
mahta/TS/TS basics/basics.ts
Outdated
return !param; | ||
} | ||
|
||
function callPropInObj(object: any, propertyName: string): void { |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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?
mahta/TS/TS basics/basics.ts
Outdated
return !param; | ||
} | ||
|
||
function callPropInObj(object: any, propertyName: string): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at the end please read this article:
https://thoughtbot.com/blog/typescript-stop-using-any-there-s-a-type-for-that
closes: #14