typescript quick start
typescript quick start
ts file -> tsc (typescript compiler) -> js file.
Try typescript
- install node.js
- node -v
- edit test.ts
var message:string = "Hello World" console.log(message)
- compile typescript file
tsc test.ts
- run the script
node test.js
- multiple files compiled at once
tsc file1.ts, file2.ts, file3.ts
Simple web application
- edit greeter.ts
class Student { fullName: string; constructor(public firstName: string, public middleInitial: string, public lastName: string) { this.fullName = firstName + " " + middleInitial + " " + lastName; } } interface Person { firstName: string; lastName: string; } function greeter(person: Person) { return "Hello, " + person.firstName + " " + person.lastName; } let user = new Student("Jane", "M.", "User"); document.body.innerHTML = greeter(user);
- compile typescript
tsc greeter.ts
- edit greeter.html ``` <!DOCTYPE html>
4. open **greeter.html** in the web browser
## Use VS Code
1. [Download and install vs code](https://code.visualstudio.com/download)
2. Right click a typescript file to show the context menu. Select **Open with code** menu item.
3. How to word wrap? **ALT+Z** or go to the menu **VIEW| Toggle Word Wrap**.
4. How to compile? **Ctrl+`** or go to the menu **VIEW|Terminal**. Type the command ```tsc .\file.ts --watch```. With **--watch** option it will auto compile when the file changed.
5. Can not find name google? If you use google map api in your code, type the commnad ```npm install --save @types/googlemaps``` to fix the problem.
6. Uncaught ReferenceError: handleLocationError is not defined google maps api? You need to use HTTPS, use a service like https://letsencrypt.org/ (It’s free, automated, and open.)
## FAQ
### add functions to windows with d ts file
1. npm install typings --global
2. Edit typings/custom/window.d.ts:
interface Window { MyNamespace: any; } declare var window: Window;
3. Install the custom typing
typings install file:typings/custom/window.d.ts –save –global
4. Use it.
window.MyNamespace = window.MyNamespace || {};
5. [reference](https://stackoverflow.com/questions/50488121/typescript-add-functions-to-window-with-d-ts-file)
### Use svg point
1. Code
let svg: SVGSVGElement =