Getting started with TypeScript and Sublime Text -- 摘自https://cmatskas.com/getting-started-with-typescript-and-sublime-text/

Getting started with TypeScript and Sublime Text

04 March 2015  18 Comments  Posted in JavaScriptOpen SourceTypeScriptSublime Text

UPDATED: This post has been rewritten around the official TypeScript plugin

Typescript is awesome, period. TypeScript, in case you don‘t know, is a superset of JavaScript that allows you to use Object Oriented principles in order to write code that can be compiled to JavaScript. In effect, you can use optional static typing, modules, interfaces and classes to write your code and once the code is compiled, the end result is an idiomatic javascript code that resembles the code that you would directly javascript if you wanted to create classes etc without the help of TypeScript. The generated javascript can run anywhere on any browser and any operating system.

TypeScript is open source and free as in beer. You can code TypeScript within Visual Studio, which offers excellent support and considers it a first class citizen or you can use the editor/IDE of your choice: Visual Studio Code EclipseVimIntelliJSublime Text and so on. New editors are constantly added and there is nothing stopping your from porting it to your favorite one. For this post, I could have used Visual Studio to start with, but this would have been too easy. Instead, I decided to go use Sublime Text so that I can show you how to set it up.

There are a few steps required in order to get up and running with TypeScript in Sublime Text so we will take it from the top, assuming you have nothing installed.

INSTALLATION

First you need to download Sublime Text 3 from http://www.sublimetext.com/3. Then you need to downloadNode.js. If you don‘t have node on your machine yet, I suggest you get it anyway as there are more and more tools that rely on node.js these days. You can get it at http://nodejs.org/download/ and you need to make sure you get the right version for your OS. With node.js installed, the next step is to install TypeScript. Open a command line and type the following:

npm install -g typescript

This will install TypeScript globally (-g) so that it can be accessed from any process, in our case Sublime Text. These three steps are the basis for starting to write TypeScript code in Sublime Text. However, you need to be able to build and compile to a .js file and in order to do this in Sublime, you need to create a customBuild System.

THE TYPESCRIPT BUILD SYSTEM

In the early days, there was a lot of manual work that had to be done to get Sublime to play well with typescript. However, this has now been resolved with an official Sublime Typescript plugin from Microsoft which works perfectly!

You can install the plugin in 2 ways:

  • Package Control --> Install Package --> TypeScript
  • Clone the repo directly into your Sublime plugin folder:
cd "%APPDATA%\Sublime Text 3\Packages"
git clone --depth 1 https://github.com/Microsoft/TypeScript-Sublime-Plugin.git TypeScript`

The plugin provides 2 very important pieces of functionality:

  • build system to compile TypeScript to pure JavaScript
  • True Intellisense.

SIDE NOTE (Skip to the next section if you know what Intellisense is) 
Intellisense is the code completion options you get when you type . The (dot) prompts intellisense to come up so depending on the type of the variable, string, number etc, you get different options. This operates in the same way that Visual Studio, Resharper and IntelliJ intellisense works, in effect, speeding up your development workflow.

WORKING WITH TYPESCRIPT WITHIN SUBLIME TEXT

With the plugin installed you can start writing your TypeScript. Create a folder anywhere you want on your file system. Open the folder in Sublime. Create a new file and name it test.ts. Paste the following code:

var testInt: number;
testInt = 5;

var test: string = "hello world";
console.log(test);

Press Ctrl+B and let the magic begin! In the Output window you should see the following:

There should also be a test.js file in the root of the folder. This is the file that your website/application can reference. Build errors will also appear at the bottom of the window, should the compilation of TypeScript fails.

TSCONFIG.JSON

What about it? As it stands, every time you add a new .ts (TypeScript) file, you get a corresponding compiled .js file. This is absolutely fine if you want to keep your files separate and reference them in different place. However, in the real world, we would be compiling all our JavaScript in one file, which then would be uglified etc through some other task (Grunt).

The TypeScript plugin now supports tsconfig.json files as well. A typical tsconfig.json looks like this:

The full documentation on tsconfig.json options can be found here

TYPESCRIPT AND SUBLIME LIKE A BOSS

I‘ll let you in a secret: you don‘t have to compile your code all the time to see the errors. The plugin comes with an excellent error window that displays errors in real time.

Ctrl+Shft+P -> Typescript: ShowErrorList

If there are any errors in your code, they will be displayed as you type! Remember that the end goal is to let you focus on what really matters, i.e. coding. The whole list of the plugin features is available through the Command Palette or using keyboard shortcuts:

Rename =>    Ctrl+T Ctrl+M
Find references =>    Ctrl+T Ctrl+R
Next reference =>    Ctrl+T Ctrl+N
Prev reference =>    Ctrl+T Ctrl+P
Format document =>    Ctrl+T Ctrl+F
Format selection =>    Ctrl+T Ctrl+F
Format line =>    Ctrl+;
Format braces =>    Ctrl+Shift ]
Navigate to symbol =>    Ctrl+ Alt R
Go to definition =>    Ctrl+T Ctrl+D or F12
Paste and format =>    Ctrl+V or Command+V
Quick info =>    Ctrl+T Ctrl+Q
Build    (Win)Ctrl+B or F7, (OSX) Command+B or F7
Error list =>    (via Command Palette)

Getting started with TypeScript and Sublime Text couldn‘t be easier. I hope you found this post useful and, as always, feel free to leave your comments or questions below.

RELATED READING

P.S. Make sure you follow me on Twitter @christosmatskas for more up-to-date news, articles and tips.

时间: 2024-10-05 05:31:36

Getting started with TypeScript and Sublime Text -- 摘自https://cmatskas.com/getting-started-with-typescript-and-sublime-text/的相关文章

线程学习笔记 等待句柄和线程池(摘自https://blog.gkarch.com/threading/part2.html#manualresetevent)

//如果你的应用有很多线程,这些线程大部分时间都在阻塞,那么可以通过调用ThreadPool.RegisterWaitForSingleObject来减少资源消耗.这个方法接受一个委托,它会在向等待句柄发信号时执行.当处于等待状态时,它不会浪费线程资源: static ManualResetEvent _starter = new ManualResetEvent (false); public static void Main() { RegisteredWaitHandle reg = Th

用swoole实现mysql的连接池--摘自https://github.com/153734009/doc/blob/master/php/mysql_pool.php

<?php   $serv = new swoole_server("0.0.0.0", 9508);   $serv->set(['worker_num'=>1, 'task_worker_num'=>5]);   function onReceive($serv, $fd, $from_id, $data)   {   $sql = $data;   $result = $serv->taskwait($sql);   if($result !== f

Laravel + go-micro + grpc 实践基于 Zipkin 的分布式链路追踪系统 摘自https://mp.weixin.qq.com/s/JkLMNabnYbod-b4syMB3Hw?

分布式调用链跟踪系统,属于监控系统的一类.系统架构逐步演进时,后期形态往往是一个平台由很多不同的服务.组件构成,用户请求过来后,可能会经过其中多个服务,如图 不过,出问题时往往很难排查,如整个请求变慢.偶尔报错.不可用等,我们很难得知具体是由哪一个或哪些服务引起的,通常开发同学都会互相甩锅,最后不得不花大量时间人肉 tracing 项目初期时,可以简单处理,通过生成唯一 request_id ,在各个方法记录日志,方便排查问题.中后期系统拆分为各个子服务时,要么继续推进原有的 request_i

Consul+upsync+Nginx实现动态负载均衡 摘自https://blog.csdn.net/qq_29247945/article/details/80787014

传统感念:每次修改完nginx配置文件,要重启nginx 动态感念:每次修改完nginx配置信息,不需要重启,nginx实时读取配置信息. Nginx: 反向代理和负载均衡 Consul:是用go编写(谷歌),实现对动态负载均衡注册与发现功能 SpringCloud支持  Zookeeper.Eureka.Consul服务注册与发现. 服务注册:服务实现者可以通过HTTP API或DNS方式,将服务注册到Consul. 服务发现:服务消费者可以通过HTTP API或DNS方式,从Consul获取

Node.js meitulu图片批量下载爬虫1.06版

//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1.06 // 1.00 完成图片爬虫,手动输入页数和目录 // 1.01 改写. // 1.02 手动输入页面url,然后自动解析 // 1.03 从命令行获得页面url,然后自动解析 // 1.04 解决数量节点位置不固定bug和输入状态不退出bug // 1.05 增加自动模式和手动模式 // 1

如何优雅地使用Sublime Text

Sublime Text:一款具有代码高亮.语法提示.自动完成且反应快速的编辑器软件,不仅具有华丽的界面,还支持插件扩展机制,用她来写代码,绝对是一种享受.相比于难于上手的Vim,浮肿沉重的Eclipse,VS,即便体积轻巧迅速启动的Editplus.Notepad++,在SublimeText面前大略显失色,无疑这款性感无比的编辑器是Coding和Writing最佳的选择,没有之一. 如何优雅地使用Sublime Text之目录: Sublime Text 2和3的对比 Sublime Tex

常用的sublime text 3插件

All Autocomplete Sublime Text 默认的 Autocomplete 功能只考虑当前的文件,而 AllAutocomplete 插件会搜索所有打开的文件来寻找匹配的提示词. SublimeCodeIntel 一个全功能的 Sublime Text 代码自动完成引擎 ,本人做过对比,但是如果和webstorm的自动寻找还是稍逊一筹,不过对于大部分人来说够用了,能很方便跳到你想要的方法 支持的语言挺多的(JavaScript, Mason, XBL, XUL, RHTML,

Sublime Text 3 全程详细图文原创教程(持续更新中。。。)

一. 前言 使用Sublime Text 也有几个年头了,版本也从2升级到3了,但犹如寒天饮冰水,冷暖尽自知.最初也是不知道从何下手,满世界地查找资料,但能查阅到的资料,苦于它们的零碎.片面,不够系统和全面,所以一路走来,耗费了本人大量的时间和精力.所以蒙生了写这篇<Sublime Text 3 全程详细指南>,一来对自己的经验是一个总结,二来可以给初学者做个系统.全面的指引,让他们少走我当时走过的弯路,从而能快速地掌握Sublime Text这个优秀的编辑器. 目前我正在使用的版本是Subl

Sublime Text 2插件[这个不错哦]

Sublime Text 2是一个轻量.简洁.高效.跨平台的编辑器,方便的配色以及兼容vim快捷键等各种优点博得了很多前端开发人员的喜爱,当然也包括我,在看到小飞的介绍后,我就一直在用了.本文推荐一些好用的插件和扩展. Sublime Text 2基本上是共享软件,免费版和收费版基本无区别,只是偶尔会弹框让你去购买,这个基本不影响使用.如果你不了解它,也可以看下小众软件的这篇详细介绍. 安装Sublime Text 2插件的方法: 1.直接安装 安装Sublime text 2插件很方便,可以直