Code Monkey home page Code Monkey logo

typescript-book's Introduction

YouTube Channel Subscribers

TypeScript Deep Dive

Learn Professional TypeScript. I've been looking at the issues that turn up commonly when people start using TypeScript. This is based on the lessons from Stack Overflow / DefinitelyTyped and general engagement with the TypeScript community. You can follow for updates and don't forget to โ˜… on GitHub ๐ŸŒน

Reviews

  • Thanks for the wonderful book. Learned a lot from it. (link)
  • Its probably the Best TypeScript book out there. Good Job (link)
  • Love how precise and clear the examples and explanations are! (link)
  • For the low, low price of free, you get pages of pure awesomeness. Chock full of source code examples and clear, concise explanations, TypeScript Deep Dive will help you learn TypeScript development. (link)
  • Just a big thank you! Best TypeScript 2 detailed explanation! (link)
  • This gitbook got my project going pronto. Fluent easy read 5 stars. (link)
  • I recommend the online #typescript book by @basarat you'll love it.(link)
  • I've always found this by @basarat really helpful. (link)
  • We must highlight TypeScript Deep Dive, an open source book.(link)
  • Great online resource for learning. (link)
  • Thank you for putting this book together, and for all your hard work within the TypeScript community. (link)
  • TypeScript Deep Dive is one of the best technical texts I've read in a while. (link)
  • Thanks @basarat for the TypeScript Deep Dive Book. Help me a lot with my first TypeScript project. (link)
  • Thanks to @basarat for this great #typescript learning resource. (link)
  • Guyz excellent book on Typescript(@typescriptlang) by @basarat (link)
  • Leaning on the legendary @basarat's "TypeScript Deep Dive" book heavily at the moment (link)
  • numTimesPointedPeopleToBasaratsTypeScriptBook++; (link)
  • A book not only for typescript, a good one for deeper JavaScript knowledge as well. link
  • In my new job, we're using @typescriptlang, which I am new to. This is insanely helpful huge thanks, @basarat! link
  • Thank you for writing TypeScript Deep Dive. I have learned so much. link
  • Loving @basarat's @typescriptlang online book basarat.gitbooks.io/typescript/# loaded with great recipes! link
  • Microsoft doc is great already, but if want to "dig deeper" into TypeScript I find this book of great value link
  • Thanks, this is a great book ๐Ÿค“๐Ÿค“ link
  • Deep dive to typescript is awesome in so many levels. i find it very insightful. Thanks link
  • @basarat's intro to @typescriptlang is still one of the best going (if not THE best) link
  • This is sweet! So many #typescript goodies! link

Get Started

If you are here to read the book online get started.

Translations

Book is completely free so you can copy paste whatever you want without requiring permission. If you have a translation you want me to link here. Send a PR.

Other Options

You can also download one of the Epub, Mobi, or PDF formats from the actions tab by clicking on the latest build run. You will find the files in the artifacts section.

Special Thanks

All the amazing contributors ๐ŸŒน

Share

Share URL: https://basarat.gitbook.io/typescript/

typescript-book's People

Contributors

basarat avatar crevil avatar csterritt avatar cthogg avatar cybermew avatar djyde avatar drake-eidukas avatar drewnoakes avatar dwillmer avatar eruizdechavez avatar felixbillon avatar henrebotha avatar imgbotapp avatar jquintozamora avatar jtcmedia avatar lifuhuang avatar marketionist avatar michalkopanski avatar myarete avatar numb86 avatar pjustino avatar randylevy avatar rootulp avatar spences10 avatar tbotaq avatar timkraut avatar tsasioglu avatar uppajung avatar wmaurer avatar xmbhasin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typescript-book's Issues

A few questions

A really good read. Thank you.

  • Could you add an example for browser using browserify?
  • You suggest using -module system but in the node and browser sections you don't use it. Why?
  • How do we use ESLint etc with typescript?
  • Can we use Typescript as a replacement for Babel and then start to add type info over time

nominalTyping.html "Using Enums" method is more restrictive than "Using Interfaces" one

The "Using Enums" method doesn't work for number types, but the "Using Interfaces" works ok for them:

enum FooIdBrand {}
type FooId = FooIdBrand & number;
enum BarIdBrand{}
type BarId = BarIdBrand & number;

var fooId: FooId;
var barId: BarId;

fooId = barId; // NO error (wrong)
barId = fooId; // NO error (wrong)

Add a note explaining this Enum method restriction.

Clarification

"If you now create a new file bar.ts in the same project, you will be allowed by the TypeScript type system to use the variable foo as if it was available globally:"

From the section on modules. What does it mean to say "in the same project"? Under the same tsconfig.json? So a project is parsed and reports named conflicts / allows sharing across files? Which implies a --out? Which in turn is actively discouraged?

It might be good to clarify that sentence along those lines. Thanks.

TIP : Singleton

Ryan's answer : http://stackoverflow.com/questions/30174078/how-to-define-singleton-in-typescript

However I prefer to have them using a once wrapper utility function which allows singletons to be lazy.

https://github.com/TypeScriptBuilder/tsb/blob/48da91c68a1977aeaa665ca609b8cf86bc32ec71/src/common/utils.ts#L73-L83

export function once<T extends Function>(func: T): T {
    let ran = false;
    let memo = undefined;
    return function() {
        if (ran) return memo;
        ran = true;
        memo = func.apply(this, arguments);
        func = null;
        return memo;
    } as any;
}

__extends. One more detail

Here you explain what does __extends do. You mentioned that it is effectively does the following:
d.prototype.__proto__ = b.prototype

From my point of view it is worth to mention why it doesn't do this directly. When I read that first time I was wondering "Why it is so compicated? Why we just can't d.prototype.__proto__ = b.prototype"

My guess that old browsers doesn't support proto property (as you mentioned). I mean that it is more safe.

Thanks for the excellent book!

Mixin

Here is a good solution : microsoft/TypeScript#6502 (comment) that uses Object.assign

microsoft/TypeScript#2919 (comment)

function Mixin<T>(...mixins) : new() => T {
    class X {
        constructor() {
            mixins[0].call(this);
        }
    }
    Object.assign(X.prototype, ...mixins.map(m => m.prototype));
    return <any>X;
}

abstract class Dog {
    bark() { console.log('bark!'); }
}

abstract class Duck {
    quack() { console.log('quack!'); }
}

class Base {
    constructor() {
        console.log('base ctor');
    }

    world = "world";

    hello() {
        console.log(`hello ${this.world}`);
    }
}

interface DoggyDuck extends Base, Dog, Duck { }

class Mutant extends Mixin<DoggyDuck>(Base, Dog, Duck) {

}

let xavier = new Mutant();
xavier.bark();
xavier.quack();
xavier.hello();

Assertion considered harmful

Destructuring - Assigning to new variable names.

This is just a suggestion, but maybe an example on how to assign properties to new variable names would be of benefit. My issue was that some of the properties contained spaces so I didn't know how to assign them.

E.G

var obj = {"some property": "some value", anotherProperty: "another value"};

//destructure
var {"some property": someProperty, anotherProperty} = obj;
//someProperty="some value"; 

Thanks for an excellent resource.

More ES6 specific features

Hi Basarat!

What do you think about adding more ES6 features, such as Symbols, Maps and Sets into the book? These features are mostly about ES6 rather than TypeScript, however usage of these things has some specific in TypeScript and worth documenting. What is the best way to structure this information on your opinion?

Computed Properties

  • In ES6 features
  • In readonly for a sample of readonly dictionary ๐ŸŒน

Language service: meaning of encoding in classifiers

The following code : https://github.com/Microsoft/TypeScript/blob/12c6a7400dffc10c41f9245da64fbd5beeceb3dc/src/services/services.ts#L7843-L7847

                if (length > 0) {
                    result.spans.push(start);
                    result.spans.push(length);
                    result.spans.push(classification);
                }

I also found the comment // Encoded as triples of [start, length, ClassificationType]. in the source. It was completely unobvious till I reviewed the code and can break on API consumers if new things are needed. Would be better if it was moved into {start,length,classification} datastructure, but I guess they did it for performance reasons ๐ŸŒน

TIP : don't use `bind`

As it is typed as any. Instead use the arrow. E.g. don't do:

<ScrollWrapper>
 <SidebarMenu toggle={ this.toggle.bind(this) } />
</ScrollWrapper>

Spread parameters

In the book, this code is demonstrated :

function foo(x, y, z) { }
var args = [0, 1, 2];
foo(...args);

This is perfectly valid ES6 code, however AFAIK the TS compiler reports an error per microsoft/TypeScript#5296

TIP Strongly typed names

getName : https://github.com/TypeStrong/atom-typescript/blob/master/lib/main/lang/utils.ts#L300-L308

var nameExtractorRegex = /return (.*);/;
/** Get the name using a lambda so that you don't have magic strings */
export function getName(nameLambda: () => any) {
    var m = nameExtractorRegex.exec(nameLambda + "");
    if (m == null)
        throw new Error("The function does not contain a statement matching 'return variableName;'");
    var access = m[1].split('.');
    return access[access.length - 1];
}

David's function : http://stackoverflow.com/a/32542368/390330

function getPropertyName(propertyFunction) {
    return /\.([^\.;]+);?\s*\}$/.exec(propertyFunction.toString())[1];
}

For lambda returns

Assert a return object literal in an expression using brackets ()=>({a:123})

Denote future features

Since this book is meant to be the definitive guide to TypeScript, developers ideally will read this when ramping up on TS as I did. In deploying a production system, you don't want to use an unreleased version of TS to get access to WIP features. You suggest getting the nightly build of TS as you are showing some future features. I would suggest that these future features (e.g., JSX, alt Type Assertion), should be marked as future to avoid confusion.

Document ES6 modules

Import

import * as $ from "jquery"
import {extend} from "jquery"
import {extend as ex} from "jquery" // Useful if the name conflicts with a local variable
import something from "es6module"; // For default import. Not recommended https://basarat.gitbooks.io/typescript/content/docs/tips/defaultIsBad.html

Export

export something
export default something // Not recommended https://basarat.gitbooks.io/typescript/content/docs/tips/defaultIsBad.html
export = something; // Not recommended for new code. Prefer default export or export individual items
export {extend as ex} // Useful when you want to extend an alias

Reexport

export * from 'mod'; // Great for top level file where you just export everthing from some sub module.
export {v} from 'mod'; // Great for top level file e.g. main.js where you just export stuff from different files in your project.
export {v as x} from 'mod'; // When you want to export stuff from someone else's module but with a different name.

String based enums

Quite often I do something like:

export var types = { 
    setActiveProject: '',
    expandErrors: '',
    collapseErrors: '',
}

// make values same as keys
Object.keys(types).map((key) => types[key] = key);

Each member is infered to be be string, you cannot access something that is invalid and the values are kept into sync.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.