TypeScript

repository·main·Indexed 13 days ago

https://github.com/microsoft/typescript

TypeScript is a typed superset of JavaScript that compiles to readable, standards-based JavaScript, designed for application-scale development. Version 6.0.0 adds optional static typing to the JavaScript ecosystem to support large-scale applications. The language provides a compiler, a language service for formatting and diagnostics, and comprehensive type definitions for ECMAScript, DOM, and Intl APIs.

Tokens
17.9K
Snippets
52
Records
83
Agent score
100%

What's inside TypeScript

  1. Understand the Microsoft Support Policy for TypeScript

    main

    TypeScript support varies depending on how you consume the project:

    • Community Support: Limited to GitHub issues, Stack Overflow (typescript tag), and the #typescript Discord channel.
    • Microsoft Product Integration: When included with a Microsoft product, TypeScript follows the Modern Support Policy.
    • Visual Studio: Servicing fixes for TypeScript versions included in under-support releases of Visual Studio are limited to security fixes.
    • Assisted Support: Professional support is available via the Microsoft assisted support team by opening a ticket.
  2. Understand the API domains in TypeScript's lib directory

    main

    The src/lib directory contains the type definitions for APIs that are part of the JavaScript language environment. These are categorized into three main domains:

    1. ECMAScript language features: Standard JavaScript APIs (e.g., methods on Array) as defined in ECMA-262.
    2. DOM APIs: APIs available in web browsers.
    3. Intl APIs: Internationalization APIs scoped to Intl as defined in ECMA-402.
  3. How TypeScript decides when to include new APIs

    main

    TypeScript follows specific criteria for adding new APIs to its library definitions to ensure stability:

    • ECMAScript & Intl APIs: Proposals must be at Stage 3 or later in the TC39 process. You can verify completed proposals via the JavaScript finished proposals list or the Intl finished proposals list.
    • DOM APIs: APIs must be available without prefixes or flags in at least two different browser engines (e.g., not just two different Chromium-based browsers).
  4. Understand the core components of TypeScript formatting

    main

    TypeScript formatting requires two primary components:

    1. A formatting context: An object containing user settings such as tab size, newline character, and other stylistic preferences.
    2. A SourceFile: The parsed representation of the code to be formatted.

    The output of a formatting operation is a collection of TextChange objects. Each object describes a specific modification to the source text by specifying the range to be replaced and the new content.

    export interface TextChange {
        span: TextSpan; // start, length
        newText: string;
    }
  5. Get started with TypeScript

    main

    TypeScript is a language that adds optional types to JavaScript, enabling better tooling for large-scale applications. It compiles to standards-based JavaScript that runs on any browser, host, or OS.

    To begin using TypeScript, you can:

  6. How to file issues and get help with TypeScript

    main

    To report bugs or request new features, use GitHub issues. Before filing a new issue, search the existing issues to prevent duplicates.

    For general questions and community help, use the following resources:

    • Stack Overflow: Use the typescript tag.
    • Discord: Join the #typescript channel.
  7. Install TypeScript via npm

    main

    You can install TypeScript using npm. To install the latest stable version as a development dependency, use the standard install command. To test the latest nightly builds, use the @next tag.

    # Install the latest stable version
    npm install -D typescript
    
    # Install nightly builds
    npm install -D typescript@next
  8. TSServer Message Protocol Overview

    main

    The TypeScript Server (TSServer) communicates using a JSON-RPC-like protocol consisting of three message types: request, response, and event.

    • Request: Sent by the client to execute a command. It includes a seq (sequence number) and a command string. Arguments are provided in the arguments field.
    • Response: Sent by the server in reply to a request. It includes the original request_seq, a success boolean, the command name, and a body containing the result if successful.
    • Event: Sent by the server to notify the client of asynchronous occurrences. It includes an event name and an optional body.
    {
      "seq": 1,
      "type": "request",
      "command": "status",
      "arguments": {}
    }
  9. How SemanticDiagnosticsBuilderProgram handles incremental diagnostics

    main

    A SemanticDiagnosticsBuilderProgram extends BuilderProgram by caching semantic diagnostics. This is useful for performance in incremental environments where you only want to re-calculate diagnostics for files that have changed or are affected by changes.

    To iterate through affected files and retrieve their diagnostics one by one, use: getSemanticDiagnosticsOfNextAffectedFile(cancellationToken?, ignoreSourceFile?).

    This method returns an AffectedFileResult<readonly Diagnostic[]>, which contains either the diagnostics and the affected SourceFile or Program, or undefined when iteration is complete.

    while (true) {
      const result = semanticBuilder.getSemanticDiagnosticsOfNextAffectedFile();
      if (!result) break;
      
      const { result: diagnostics, affected } = result;
      console.log(`File ${affected.fileName} has ${diagnostics.length} diagnostics`);
    }
  10. Understand RegExp Character Class Syntax and Errors

    main

    The TypeScript scanner implements complex Regular Expression syntax, including Unicode Sets (the v flag) and character class operations. When writing regular expressions, be aware of the following constraints enforced by the scanner:

    • Character Class Ranges: A range like [a-z] must have a minimum character value less than or equal to the maximum. If the range is out of order (e.g., [z-a]), a Range_out_of_order_in_character_class error is reported.
    • Negated Character Classes: Inside a negated class (e.g., [^...]), you cannot use elements that might match more than a single character (like certain Unicode property expressions) if they could potentially match a string.
    • Set Operations: The scanner supports ClassUnion (default), ClassIntersection (&&), and ClassSubtraction (--). Mixing these operators (e.g., using both && and -- in the same class) is invalid and triggers Operators_must_not_be_mixed_within_a_character_class.
    • Reserved Punctuators: Certain characters like (, ), [, ], {, }, -, and | are reserved in character classes and must be escaped with a backslash if used as literals.
    • Unicode Property Escapes: Using \p{...} or \P{...} requires the u (Unicode) or v (Unicode Sets) flag. If these flags are missing, the scanner reports that these expressions are only available with those flags.