total-typescript / beginners-typescript-tutorial Goto Github PK
View Code? Open in Web Editor NEWAn interactive TypeScript tutorial for beginners
Home Page: https://totaltypescript.com
An interactive TypeScript tutorial for beginners
Home Page: https://totaltypescript.com
I have cloned the project and tried to install packages. But I got the following error:
error F:\code\beginners-typescript-tutorial\node_modules\esbuild: Command failed.
Exit code: 1
Command: node install.js
Arguments:
Directory: F:\code\beginners-typescript-tutorial\node_modules\esbuild
Output:
node:internal/errors:841
const err = new Error(message);
type AddTwoNumbersArgs = {
first: number;
second: number;
};
type AddTwoNumbersFn = (params: AddTwoNumbersArgs) => number;
export const addTwoNumbers: AddTwoNumbersFn = (params) => {
return params.first + params.second;
};
What about adding a corresponding markdown file for each lesson. This would essentially be what you would be saying if you were teaching it as you did on livestream.
We need to tell Typescript what type of data are expect for parameters a and b in the addTwoNumbers function. The format to declare the type of a function parameter is to add : type
following each parameter. In this case it would be : number
. With this information, Typescript can go to work enforcing appropriate typing of the addTwoNumbers function parameters.
Even better, format the makdown file so that the top describes the problem/expectations and then a link scrolls down to describe the answer.
Getting the following error after running any exercise (tests are passing) inside VS Code :
Checking types... error TS5023: Unknown compiler option '-'. Failed. Try again!
Providing a default user that matches the shape of User
without actually providing a return type annotation passes the tests and typecheck, but that obviously avoids the intended lesson.
const makeUser = () => {
return {
id: 1,
firstName: '',
lastName: '',
role: 'user',
posts: [
{
id: 1,
title: '',
},
],
}
}
There's an unhandled error when starting the course:
Running tests...
failed to load config from /home/projects/kkvioyjzk.github/vite.config.ts
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: The package "esbuild-linux-64" could not be found, and is needed by esbuild.
If you are installing esbuild with npm, make sure that you don't specify the
"--no-optional" flag. The "optionalDependencies" package.json feature is used
by esbuild to install the correct binary executable for your current platform.
at generateBinPath (file:///home/projects/kkvioyjzk.github/node_modules/esbuild/lib/main.js:1843:15)
at esbuildCommandAndArgs (file:///home/projects/kkvioyjzk.github/node_modules/esbuild/lib/main.js:1901:31)
at ensureServiceIsRunning (file:///home/projects/kkvioyjzk.github/node_modules/esbuild/lib/main.js:2065:25)
at Module.build (file:///home/projects/kkvioyjzk.github/node_modules/esbuild/lib/main.js:1956:26)
at bundleConfigFile (file://file:///home/projects/kkvioyjzk.github/node_modules/vite/dist/node/chunks/dep-5cb728cb.js:62943:35)
at loadConfigFromFile (file://file:///home/projects/kkvioyjzk.github/node_modules/vite/dist/node/chunks/dep-5cb728cb.js:62919:31)
at resolveConfig (file://file:///home/projects/kkvioyjzk.github/node_modules/vite/dist/node/chunks/dep-5cb728cb.js:62534:34)
at Module.createServer (file://file:///home/projects/kkvioyjzk.github/node_modules/vite/dist/node/chunks/dep-5cb728cb.js:59137:26)
at createVitest (file://file:///home/projects/kkvioyjzk.github/node_modules/vitest/dist/chunk-node-pkg.30d8b37e.js:13867:30)
at async Module.startVitest (file://file:///home/projects/kkvioyjzk.github/node_modules/vitest/dist/chunk-node-pkg.30d8b37e.js:20259:15)
Failed. Try again!
const coerceAmount = (amount: number | { amount: number }) => {
return 20;
};
it("Should return the amount when passed an object", () => {
expect(coerceAmount({ amount: 20 })).toEqual(20);
});
it("Should return the amount when passed a number", () => {
expect(coerceAmount(20)).toEqual(20);
});
Ex 12 can be passed with just returning 20, perhaps the tests should use two different numbers to make this work around less obvious?
when i try to run the first exercise, I get the following error messages
Running tests...
RUN v0.21.1 /home/elementary/Downloads/beginners-typescript-tutorial
✓ src/01-number.problem.ts (1)
Test Files 1 passed (1)
Tests 1 passed (1)
Start at 14:16:57
Duration 1.30s (setup 62ms, collect 27ms, tests 5ms)
Checking types...
src/01-number.problem.ts:3:31 - error TS7006: Parameter 'a' implicitly has an 'any' type.
3 export const addTwoNumbers = (a, b) => {
~
src/01-number.problem.ts:3:34 - error TS7006: Parameter 'b' implicitly has an 'any' type.
3 export const addTwoNumbers = (a, b) => {
~
Found 2 errors in the same file, starting at: src/01-number.problem.ts:3
I've tried settings tsconfig.json
's noImplicityAny' and
strict` options to 'false' as advisable on the internet, but the error still persists. Any help?
First of all thank you for making this! It will definitely be helpful for a lot of people 🙌
I've just went through the exercises and in the number 9, I realized that there isn't any kind of description of the problem or hint on how to solve it. I guess it would probably be hard for a beginner to realize what should be done in that case.
I'd like to hear your thoughts on this.
Hey @mattpocock .
Thanks for your effort and time spent on this course.
I'd suggest adding Partial
Example.
What do you think of that?
/**
* How do we make _only_ the id property mandatory?
*/
interface User {
id: string;
firstName: string;
lastName: string;
address: string;
age: number;
sex: string;
}
/**
* Answer1 - optional properties
*/
interface User {
id: string;
firstName?: string;
lastName?: string;
address?: string;
age?: number;
sex?: string;
};
/**
* Answer2 - Partial properties
*/
interface User {
id: string;
} & Partial<{
firstName: string;
lastName: string;
address: string;
age: number;
sex: string;
}>
Hey!!
First of, thanks for the great and very useful initiative!
I would like to suggest adding a .nvmrc
file to the project. The reason is that, on my case, I have an older node version installed and used by default. When trying to run the first exercise I would get this error:
argv.passWithNoTests ?? (argv.passWithNoTests = true);
^
SyntaxError: Unexpected token '?'
This is related to vite configs I guess, but since installing dependencies didn't throw any error took me a few minutes to realize it was the node version. Adding the preferred node version to the project would help to identify this earlier.
:)
Hey!
Superconfusing error I get when I download the project and install it. When running the install and then an exercise I get this message:
../../../../node_modules/@types/react-dom/index.d.ts:20:19 - error TS2724: '"react"' has no exported member named 'SFCElement'. Did you mean 'CElement'
But I can't even find the @types/react-dom/index.d.ts file.
Awesome project, can't wait to jump in.
Was confused when VS Code opened up in high zoom level with the nav bar on the right. I think it is best to not mess with user settings too much and just leave it up to the user. Maybe add the .vscode
folder to .gitignore
or something.
Thanks!
This seems so simple that maybe I'm overthinking it and wrong? Either way, I'm not sure why we don't test the case that it works with just the last name as well.
Hey @mattpocock .
Thanks for your effort and time spent on this course.
I just want to point out that you can fool typechecker in this exercise with simply typing this, and it will pass the test.
role: string[]
instead of role: 'admin' | 'user' | 'super-admin'
The license file
indicate how changes can be made to your repo. If included, it helps contributors know how you want pull requests and issues handled.
.....
interface User {
id: string;
firstName: string;
lastName: string;
}
interface Post {
id: User["id"];
title: string;
body: string;
}
interface Comment {
id: User["id"];
comment: string;
}
I came up with a slightly different solution to problem 15-intersection which follows from problem 14-extends.
interface UserWithPosts extends User {
posts: Post[]
}
This interface also passes the type check and it creates a slightly different code completion on WebStorm (at least)
There is no massive difference between the 2 but I thought I'd mention it. I imagine using the &
operator with type
is like using the extends
with interfaces
. They do the same as far as I can tell.
Thanks a ton for the great content! :)
Dear @mattpocock ,
thanks for making this free contents and for all the efforts you put into making videos on youtube.
Keep up the good work!
Regards,
Daniele
The file 02-object-param.solution.3.ts is a duplicate of 02-object-param.solution.2.ts. Is this intentional? I recommend removing it. I open this PR to resolve the issue. #44
Both the code of conduct and contributing guidelines
are not added to the root directory.
I can work on them.
Thank you, Matt, for your wonderful youtube videos!
Maybe I'm missing something, but it seems that solution no. 2 for exercise 08 does not "ensure that makeUser ALWAYS returns a User
"(if the returned object was accidentally modified incorrectly)?
👋 Hello,
On this page https://www.totaltypescript.com/tutorials/beginners-typescript/combining-types-to-create-new-types/solution
if I use as suggested at the end
type UserAndPosts = (): User & { posts: Post[] }
export const getDefaultUserAndPosts = (): UserAndPosts => {
instead of
export const getDefaultUserAndPosts = (): User & { posts: Post[] } => {
I got errors: src/15-intersection.problem.ts:16:23 - error TS1005: '=>' expected.
What is wrong with it?
interface OnFocusChange {
(isFocused: boolean): void;
}
This could be construed as a tuple of type Post
, but of course it's an array.
beginners-typescript-tutorial/src/07-arrays.problem.ts
Lines 19 to 24 in 266c2f6
It might be better to create an array example so it fails if a student set the user interface to [Post]
which passes the type checks.
posts: [
{
id: 1,
title: "How I eat so much cheese",
},
{
id: 2,
title: "Why I don't eat vegetables",
},
],
Possibly related to #3 (wrong explanation included?)
I think this exercise might warrant a comment above/within User
that points out that some change should be made to User
in order to resolve the type error, similar to the comment in 06 noting that changing the role
property is the way to solve the problem.
Hi!
In the spirit of providing alternate solutions
for exercice 13 (entitled catch-blocks
),
does it make sense to encourage possible introduction of
unknown
typee
variable inheritingError
class so that it is handlede
variableError
class?A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.