Giải thích <Square>{} có nghĩa gì??? (ok)
https://stackoverflow.com/questions/38831342/what-does-enclosing-a-class-in-angle-brackets-mean-in-typescript
Ví dụ đã hoàn thành :) chú ý tôi thấy nó chạy tốt trong môi trường reactjs nhưng mội trường chỉ có typescript nó lỗi :(
Chú ý nếu nhìn thấy lỗi này khi biên dịch :)
index.tsx(1,1): error TS1208: 'index.tsx' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
Under the hood, ts-node
takes your script, does some semantic checking to ensure your code is error-free, and then compiles your TypeScript into JavaScript.
This is the safest option. But if you’re not worried about TypeScript errors, you can pass in the -T
or --transpileOnly
flag. This flag tells ts-node
to transpile down to JavaScript without checking for any TypeScript errors.
While it’s not always advisable to use this flag, there are scenarios where it makes sense. If you’re trying to run someone else’s script or if you’re confident that your editor and linter are catching everything, using the -T
or --transpileOnly
flag is appropriate.
npx ts-node -T reptile.ts
Ask QuestionAsked 4 years, 3 months agoActive 2 years, 7 months agoViewed 20k times5114
I am very new to TypeScript and I am loving it a lot, especially how easy it is to do OOP in Javascript. I am however stuck on trying to figure out the semantics when it comes to using angle brackets.
From their docs, I have seen several example like
and
I am having trouble understanding what this exactly means or the way to think of/understand it.
1I found this question looking for a more comprehensive understanding of angle brackets in Typescript, and this question provides information on more uses for angle brackets: stackoverflow.com/a/37369249/6691051 – Toivo Säwén Dec 19 '19 at 11:16
2 Answers
That's called Type Assertion or casting.
These are the same:
Example:
So you can do:
Or:
Which will do the same
Last updated
Was this helpful?