0% found this document useful (0 votes)
14 views35 pages

Typescript

The document contains various code snippets related to Docker and TypeScript, including commands for running Node.js containers, installing TypeScript globally, and setting up development environments. It also provides examples of using the Intl.DisplayNames API and TypeScript's incremental build watcher. Each snippet includes a title, description, source link, and the corresponding code in different programming languages.

Uploaded by

anonimo.crush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views35 pages

Typescript

The document contains various code snippets related to Docker and TypeScript, including commands for running Node.js containers, installing TypeScript globally, and setting up development environments. It also provides examples of using the Intl.DisplayNames API and TypeScript's incremental build watcher. Each snippet includes a title, description, source link, and the corresponding code in different programming languages.

Uploaded by

anonimo.crush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 35

========================

CODE SNIPPETS
========================
TITLE: Run a [Link] Container
DESCRIPTION: Starts an interactive, pseudo-TTY session in a new [Link] container.
The container is automatically removed upon exit. This is a basic command to get a
running [Link] environment.

SOURCE: [Link]
[Link]#_snippet_0

LANGUAGE: docker
CODE:
```
docker run -it --rm node
```

----------------------------------------

TITLE: Docker Setup for Tsserverfuzzer


DESCRIPTION: Clones the tsserverfuzzer repository and starts a Docker container. It
mounts the current directory as /fuzzer, sets the working directory, and runs as
the 'node' user. Supports Windows %cd% and PowerShell $pwd for directory mounting.

SOURCE: [Link]
[Link]#_snippet_5

LANGUAGE: bash
CODE:
```
c:\> cd work
C:\work> git clone ...tsserverfuzzer...
C:\work> cd tsserverfuzzer
C:\work\tsserverfuzzer> docker run -it --rm -v %cd%:/fuzzer -w /fuzzer -u node node
bash
```

LANGUAGE: powershell
CODE:
```
PS C:\work> cd work
PS C:\work> git clone ...tsserverfuzzer...
PS C:\work> cd tsserverfuzzer
PS C:\work\tsserverfuzzer> docker run -it --rm -v $pwd:/fuzzer -w /fuzzer -u node
node bash
```

----------------------------------------

TITLE: Install and Link TypeScript Globally


DESCRIPTION: Installs the TypeScript compiler globally using npm and links it to
the project's environment. This is a prerequisite for using the TypeScript compiler
API.

SOURCE: [Link]
Compiler-API-(TypeScript-1.4).md#_snippet_0

LANGUAGE: bash
CODE:
```
npm install -g typescript
npm link typescript
```

----------------------------------------

TITLE: Install [Link] Declaration Files


DESCRIPTION: Installs the TypeScript declaration files for [Link], which are
necessary for using [Link] APIs in TypeScript projects and examples.

SOURCE: [Link]
[Link]#_snippet_1

LANGUAGE: sh
CODE:
```
npm install -D @types/node
```

----------------------------------------

TITLE: Install and Run Gollum Wiki


DESCRIPTION: Instructions to install Gollum, a static site generator, and start the
local wiki server. This allows for local development and testing of wiki content.

SOURCE:
[Link]

LANGUAGE: sh
CODE:
```
# Install the deps
gem install gollum

# Start the server


gollum
```

----------------------------------------

TITLE: Install Latest TypeScript Version


DESCRIPTION: Installs the latest version of TypeScript using npm. This is a
prerequisite for using the `--generateTrace` feature, which was introduced in
TypeScript 4.1.

SOURCE: [Link]
[Link]#_snippet_0

LANGUAGE: sh
CODE:
```
npm install typescript@latest
```

----------------------------------------

TITLE: Example Usage: Machine without Provided Actors


DESCRIPTION: Shows a basic example of creating a machine using `setup` when no
specific actors are provided, demonstrating the flexibility of the system.

SOURCE: [Link]
reference/[Link]#_snippet_2

LANGUAGE: typescript
CODE:
```
// no provided actors, `assign` should still work
setup().createMachine({
entry: assign(() => ({})),
});

```

----------------------------------------

TITLE: Install and Link TypeScript Globally


DESCRIPTION: Installs the TypeScript compiler globally using npm and links it to
the local project environment. This is a prerequisite for using TypeScript from the
command line or programmatically.

SOURCE: [Link]
[Link]#_snippet_0

LANGUAGE: sh
CODE:
```
npm install -g typescript
npm link typescript
```

----------------------------------------

TITLE: TypeScript Project Setup and Testing


DESCRIPTION: A sequence of bash commands to set up the TypeScript development
environment, install dependencies, and run tests.

SOURCE:
[Link]

LANGUAGE: bash
CODE:
```
cd TypeScript
npm ci
hereby runtests-parallel
```

----------------------------------------

TITLE: Mounting .npmrc for Containerized npm Install


DESCRIPTION: Mounts the user's local .npmrc file into the Docker container in read-
only mode. This allows the container to use the host's npm authentication
credentials for installing packages.

SOURCE: [Link]
[Link]#_snippet_10
LANGUAGE: dockerfile
CODE:
```
docker run ... -v %USERPROFILE%\.npmrc:/home/node/.npmrc:ro ...
```

----------------------------------------

TITLE: Installing Sudo in Minimal Node Docker Image


DESCRIPTION: Installs 'sudo' and updates package lists within a minimal [Link]
Docker container. This is a workaround for code that requires elevated privileges,
as the base image does not include sudo by default.

SOURCE: [Link]
[Link]#_snippet_11

LANGUAGE: bash
CODE:
```
docker run -it --rm ... node bash
# Inside container:
# apt update; apt install sudo
# Then run your script:
# node /work/[Link] 1 3.3 3.4 false
```

----------------------------------------

TITLE: Analyze TypeScript Trace with analyze-trace


DESCRIPTION: Installs and executes the `@typescript/analyze-trace` package to
provide a quick summary of potential performance issues identified in the generated
trace files. The `some_directory` argument should point to the directory where
trace files were generated.

SOURCE: [Link]
[Link]#_snippet_2

LANGUAGE: sh
CODE:
```
npm install @typescript/analyze-trace
npx analyze-trace some_directory
```

----------------------------------------

TITLE: Run Container as Specific User and Directory


DESCRIPTION: Starts an interactive `bash` session in a [Link] container, running
as the `node` user and setting the working directory to `/home/node`. This is
useful for simulating user-specific environments.

SOURCE: [Link]
[Link]#_snippet_3

LANGUAGE: docker
CODE:
```
docker run -it --rm -u node -w /home/node node bash
```
----------------------------------------

TITLE: Executing Commands in a Running Docker Container


DESCRIPTION: This example shows how to start an interactive bash shell within a
running Docker container. First, you use `docker ps` to find the container ID, then
`docker exec -it <container-id> bash` to enter the container's environment.

SOURCE: [Link]
[Link]#_snippet_14

LANGUAGE: shell
CODE:
```
C:\> docker ps
... node id ...
C:\> docker exec -it 123 bash
```

----------------------------------------

TITLE: Running Tsserverfuzzer Commands in Docker


DESCRIPTION: Executes essential commands within the Docker container: installing
[Link] dependencies, building the project, checking git status, and running the
main fuzzer script.

SOURCE: [Link]
[Link]#_snippet_6

LANGUAGE: bash
CODE:
```
node@...:/fuzzer$ npm install
...
node@...:/fuzzer$ npm run build
...
node@...:/fuzzer$ git status
...
node@...:/fuzzer$ node lib/Fuzzer/[Link]
```

----------------------------------------

TITLE: Run Container with Mounted Volume


DESCRIPTION: Starts an interactive `bash` session in a [Link] container, mounting
the host's `c:\foo` directory to `/work` inside the container. Changes made in
`/work` will persist on the host.

SOURCE: [Link]
[Link]#_snippet_4

LANGUAGE: docker
CODE:
```
docker run -it --rm -v c:\foo:/work node bash
```

----------------------------------------
TITLE: Run Container with Custom Command
DESCRIPTION: Starts an interactive session in a [Link] container and overrides the
default entrypoint to launch a `bash` shell. This allows executing custom commands
within the container before it exits.

SOURCE: [Link]
[Link]#_snippet_2

LANGUAGE: docker
CODE:
```
docker run -it --rm node bash
```

----------------------------------------

TITLE: Run a Specific [Link] Version Container


DESCRIPTION: Starts an interactive session in a [Link] container, specifically
using the `12` tag for the image. If no tag is specified, `:latest` is used by
default. The container is removed after use.

SOURCE: [Link]
[Link]#_snippet_1

LANGUAGE: docker
CODE:
```
docker run -it --rm node:12
```

----------------------------------------

TITLE: Docker Port Forwarding for Debugging


DESCRIPTION: Starts a Docker container and forwards port 9242 from the container to
port 9229 on the host. This is crucial for attaching a debugger from the host
machine to the process running inside the container.

SOURCE: [Link]
[Link]#_snippet_7

LANGUAGE: bash
CODE:
```
docker run ...same... -p 9229:9242 node bash
```

----------------------------------------

TITLE: React JSX Setup Example


DESCRIPTION: A valid example of setting up JSX with React in a TypeScript file,
demonstrating the `jsxImportSource` directive and basic JSX structure. This serves
as a reference for correct configuration.

SOURCE: [Link]
reference/jsxJsxsCjsTransformCustomImportPragma(jsx=react-
jsxdev).[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
/// <reference path="/.lib/[Link]" />
/* @jsxImportSource react */
import "./preact";
const a = <>
<p></p>
text
<div className="foo"></div>
</>

export {};
```

----------------------------------------

TITLE: JavaScript Example with Docker GUI Reference


DESCRIPTION: This snippet demonstrates a JavaScript string concatenation,
referencing the availability of a Docker GUI for Windows users. It's a simple
example illustrating how JavaScript might be used in conjunction with Docker
concepts.

SOURCE: [Link]
[Link]#_snippet_12

LANGUAGE: javascript
CODE:
```
const orGUI =
"Or, as long as you're a gui-dependent windows user,"
+ "just use the docker gui...";

[Link](orGUI);
```

----------------------------------------

TITLE: TypeScript: Incremental Build Watcher


DESCRIPTION: This TypeScript code snippet demonstrates how to create an incremental
build system. It sets up a file watcher using [Link]'s `fs` module and the
TypeScript Language Service to detect file changes, update file versions, and re-
emit compiled outputs. The example includes error handling for compilation
diagnostics.

SOURCE: [Link]
Compiler-API-(TypeScript-1.4).md#_snippet_4

LANGUAGE: TypeScript
CODE:
```
/// <reference path="typings/node/[Link]" />
/// <reference path="typings/typescript/[Link]" />

import fs = require("fs");
import ts = require("typescript");
import path = require("path");

function watch(filenames: string[], options: [Link]) {


var files: [Link]<{ version: number; text: string; }> = {};
// Add the default library file
[Link]([Link]([Link]([Link]('typescript')),
'[Link]'));

// initialize the list of files


[Link](filename => {
files[filename] = { version: 0, text:
[Link](filename).toString() };
});

// Create the language service host to allow the LS to communicate with the
host
var servicesHost: [Link] = {
getScriptFileNames: () => filenames,
getScriptVersion: (filename) => files[filename] &&
files[filename].[Link](),
getScriptSnapshot: (filename) => {
var file = files[filename];
return {
getText: (start, end) => [Link](start, end),
getLength: () => [Link],
getLineStartPositions: () => [],
getChangeRange: (oldSnapshot) => undefined
};
},
getCurrentDirectory: () => [Link](),
getScriptIsOpen: () => true,
getCompilationSettings: () => options,
getDefaultLibFilename:(options) => '[Link]',
log: (message) => [Link](message)
};

// Create the language service files


var services = [Link](servicesHost,
[Link]())

// Now let's watch the files


[Link](filename => {
// First time around, emit all files
emitFile(filename);

// Add a watch on the file to handle next change


[Link](filename,
{ persistent: true, interval: 250 },
(curr, prev) => {
// Check timestamp
if (+[Link] <= +[Link]) {
return;
}

var file = files[filename];

// Update the version to signal a change in the file


[Link]++;

// Clear the text to force a new read


[Link] = [Link](filename).toString();
// write the changes to disk
emitFile(filename);
});
});

function emitFile(filename: string) {


var output = [Link](filename);

if ([Link] === [Link]) {


[Link](`Emitting ${filename}`);
}
else {
[Link](`Emitting ${filename} failed`);
var allDiagnostics = [Link]()
.concat([Link](filename))
.concat([Link](filename));

[Link](diagnostic => {
var lineChar =
[Link]([Link]);
[Link](` ${[Link] && [Link]} ($
{[Link]},${[Link]}): ${[Link]}`);
});
}

[Link](o => {
[Link]([Link], [Link], "utf8");
});
}
}

// Initialize files constituting the program as all .ts files in the current
directory
var currentDirectoryFiles = [Link]([Link]()).
filter(filename=> [Link] >= 3 && [Link]([Link] - 3,
3) === ".ts");
//map(filename => [Link]([Link](), filename));

// Start the watcher


watch(currentDirectoryFiles, { target: [Link].ES5, module:
[Link] });

```

----------------------------------------

TITLE: Intl DisplayNames API


DESCRIPTION: Demonstrates the [Link] API for getting human-readable
names for locales, regions, scripts, and languages. Includes examples of creating
DisplayNames instances and using the 'of' method.

SOURCE: [Link]
reference/[Link]#_snippet_2

LANGUAGE: APIDOC
CODE:
```
[Link](locales?: string | string[], options?: DisplayNamesOptions)
Provides human-readable names for BCP 47 language tags.
Parameters:
locales: A string with a BCP 47 language tag, or an array of such tags.
options: An object with properties like 'type' (e.g., 'region', 'language',
'script', 'currency') and 'style'.

of(code: string): string | undefined


Returns the display name for a given code.
Parameters:
code: The code (e.g., 'US', 'en', 'Latn') to look up.

Example Usage:
const regionNamesInEnglish = new [Link](['en'], { type: 'region' });
[Link]([Link]('US')); // Expected: "United States"

const regionNamesInTraditionalChinese = new [Link](['zh-Hant'], { type:


'region' });
[Link]([Link]('US')); // Expected: "美國"

// Example with type 'language'


[Link]((new [Link](undefined, {type: 'language'})).of('en-GB')); //
Expected: "British English"
```

----------------------------------------

TITLE: JSX Fragment Opening Tag


DESCRIPTION: Demonstrates the basic syntax for an opening JSX fragment tag in
TypeScript. This snippet shows a common starting point for JSX structures.

SOURCE: [Link]
reference/[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
const jsx = <>

```

----------------------------------------

TITLE: Example Usage: Actor Spawning with Type Checking


DESCRIPTION: Demonstrates how to use the `setup` function to create a machine and
spawn actors, highlighting a type error when an incorrect actor type is provided to
`spawn`.

SOURCE: [Link]
reference/[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
declare const counterLogic: ActorLogic<{ type: "INCREMENT" }>;

// example usage
setup({
actors: { counter: counterLogic },
}).createMachine({
entry: assign((spawn) => {
spawn("counter"); // ok
spawn("alarm"); // error
~~~~~~~
!!! error TS2345: Argument of type '"alarm"' is not assignable to parameter of type
'"counter"'.
return {};
}),
});

```

----------------------------------------

TITLE: Baseline Module: Prepare Baseline Report


DESCRIPTION: Prepares the content for the baseline report HTML file. It either
reads existing content from '[Link]' or starts with a predefined HTML
header, removing the trailer if reading existing content.

SOURCE: [Link]
reference/[Link]#_snippet_72

LANGUAGE: typescript
CODE:
```
function prepareBaselineReport(): string {
var reportContent = htmlLeader;
// Delete the [Link] file if needed
if ([Link](reportFilename)) {
reportContent = [Link](reportFilename);
reportContent = [Link](htmlTrailer, '');
} else {
reportContent = htmlLeader;
}
return reportContent;
}
```

----------------------------------------

TITLE: Valid React JSX Import Source


DESCRIPTION: Demonstrates a correct setup for React JSX using the `/*
@jsxImportSource react */` pragma. This example shows how to import components and
use JSX syntax with React, assuming the necessary types and runtime are available.

SOURCE: [Link]
reference/jsxJsxsCjsTransformKeyPropCustomImportPragma(jsx=react-
jsx).[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
/// <reference path="/.lib/[Link]" />
/* @jsxImportSource react */
import "./preact";
const props2 = { answer: 42 }
const a2 = <div key="foo" {...props2}>text</div>;
const b2 = <div {...props2} key="bar">text</div>;
export {};

```

----------------------------------------

TITLE: Install Dependencies


DESCRIPTION: Installs all necessary project dependencies using npm ci, which is
recommended for CI environments or when ensuring a clean install.

SOURCE:
[Link]

LANGUAGE: bash
CODE:
```
npm ci
```

----------------------------------------

TITLE: TypeScript ESM to ESM Import Example


DESCRIPTION: Shows a standard ECMAScript module importing other ECMAScript modules
using defined import maps. This setup allows for cleaner module referencing within
the project.

SOURCE: [Link]
reference/nodeModulesPackageImports(module=node16).[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
// esm format file
import * as cjs from "#cjs";
import * as mjs from "#mjs";
import * as type from "#type";
cjs;
mjs;
type;
```

----------------------------------------

TITLE: TypeScript MSI Installer File Locations


DESCRIPTION: Describes the installation paths for TypeScript files when using the
MSI installer on Windows, differentiating between versions before and after 2.3.

SOURCE: [Link]
[Link]#_snippet_1

LANGUAGE: text
CODE:
```
Microsoft SDKs Folder:
"%ProgramFiles%\Microsoft SDKs\TypeScript\<version-number>"
(contains [Link] and dependencies)
"%ProgramFiles%\Microsoft SDKs\TypeScript\<version-number>\build"
(versions 2.3+; contains [Link], [Link],
dependencies)
"%ProgramFiles%\Microsoft SDKs\TypeScript\<version-number>\versions"
(versions 2.3+; used by stub target file)

MSBuild folder:
"%ProgramFile%\MSBuild\Microsoft\VisualStudio\v14.0\TypeScript"
(contains stub target file for versions 2.3+)
```

----------------------------------------

TITLE: JSX Fragment with Invalid Start Character


DESCRIPTION: Shows a syntax error where a JSX fragment starts with an unexpected
character.

SOURCE: [Link]
reference/[Link]#_snippet_12

LANGUAGE: typescript
CODE:
```
const jsx = > </>;

```

----------------------------------------

TITLE: TypeScript ESM to ESM Import Example


DESCRIPTION: Shows a standard ECMAScript module importing other ECMAScript modules
using defined import maps. This setup allows for cleaner module referencing within
the project.

SOURCE: [Link]
reference/nodeModulesPackageImports(module=node18).[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
// esm format file
import * as cjs from "#cjs";
import * as mjs from "#mjs";
import * as type from "#type";
cjs;
mjs;
type;
```

----------------------------------------

TITLE: TypeScript: Get Accessor Cannot Have Parameters or Must Return Value
DESCRIPTION: This snippet illustrates TypeScript errors TS1054 and TS2378. TS1054
indicates that a 'get' accessor in a class cannot accept parameters. TS2378
signifies that a 'get' accessor must always return a value, which is not satisfied
in the example.

SOURCE: [Link]
reference/[Link]#_snippet_0

LANGUAGE: TypeScript
CODE:
```
class C {
get Foo(a: number) { }
~~~
!!! error TS1054: A 'get' accessor cannot have parameters.
~~~
!!! error TS2378: A 'get' accessor must return a value.
}

```

----------------------------------------

TITLE: Install TypeScript and Locate tsserver


DESCRIPTION: Installs the TypeScript package via npm and demonstrates how to locate
the tsserver executable within the installed package.

SOURCE: [Link]
Server-(tsserver).md#_snippet_0

LANGUAGE: cmd
CODE:
```
npm install --save typescript
ls node_modules\typescript\lib\[Link]
```

----------------------------------------

TITLE: Get TypeScript Version


DESCRIPTION: Command to check the currently installed version of the TypeScript
compiler.

SOURCE:
[Link]

LANGUAGE: bash
CODE:
```
tsc --v
```

----------------------------------------

TITLE: TypeScript Main Program Execution


DESCRIPTION: Represents a call to the Main method within the Program class of
TypeScriptAllInOne, typically used to start application execution.

SOURCE: [Link]
reference/[Link]#_snippet_15

LANGUAGE: TypeScript
CODE:
```
[Link]();
```

----------------------------------------
TITLE: TypeScript Bot Command Example
DESCRIPTION: Demonstrates how to invoke the TypeScript bot by issuing a command
within a GitHub comment. This is a user-facing interaction example.

SOURCE: [Link]
[Link]#_snippet_0

LANGUAGE: markdown
CODE:
```
@typescript-bot pack this
```

----------------------------------------

TITLE: Define Decorator Variable in TypeScript


DESCRIPTION: Defines a variable `dec` intended for use as a TypeScript decorator.
This setup is a prerequisite for applying custom decorators in the following
examples.

SOURCE: [Link]
reference/[Link]#_snippet_0

LANGUAGE: TypeScript
CODE:
```
var dec;
```

----------------------------------------

TITLE: Example TypeScript Class for Documentation


DESCRIPTION: This is an example of a TypeScript class with JSDoc comments for the
class itself and its constructor parameters. This input demonstrates the structure
expected by the documentation generator script.

SOURCE: [Link]
[Link]#_snippet_15

LANGUAGE: typescript
CODE:
```
/**
* Documentation for C
*/
class C {
/**
* constructor documentation
* @param a my parameter documentation
* @param b another parameter documentation
*/
constructor(a: string, b: C) { }
}

```

----------------------------------------

TITLE: Empty TypeScript File


DESCRIPTION: An empty TypeScript file used as a placeholder or for testing harness
setup. It contains only an export statement to ensure it's treated as a module.

SOURCE: [Link]
reference/[Link]#_snippet_15

LANGUAGE: typescript
CODE:
```
export {}; // Silly test harness
```

----------------------------------------

TITLE: Basic TypeScript Import Example


DESCRIPTION: A simple TypeScript file demonstrating how to import a module. This
snippet shows the basic syntax for importing external modules, which is fundamental
for organizing code in TypeScript projects.

SOURCE: [Link]
reference/[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
import 'someModule';
```

----------------------------------------

TITLE: TypeScript: Calling Get Accessors


DESCRIPTION: Demonstrates TypeScript error TS6234 where a 'get' accessor is
incorrectly called as a function. This occurs when trying to invoke a property that
is defined with a getter, leading to a 'not callable' error. The examples show this
in both non-generic and generic class contexts.

SOURCE: [Link]
reference/[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
module NonGeneric {
class C {
x: string;
get y() {
return 1;
}
set y(v) { }
fn() { return this; }
constructor(public a: number, private b: number) { }
}

var c = new C(1, 2);


var r = [Link]();
var r2 = r.x;
var r3 = r.y;
r.y = 4;
var r6 = c.y(); // error
~
```

LANGUAGE: typescript
CODE:
```
module Generic {
class C<T,U> {
x: T;
get y() {
return null;
}
set y(v: U) { }
fn() { return this; }
constructor(public a: T, private b: U) { }
}

var c = new C(1, '');


var r = [Link]();
var r2 = r.x;
var r3 = r.y;
r.y = '';
var r6 = c.y(); // error
~
```

----------------------------------------

TITLE: TypeScript typeof on Classes and Instances


DESCRIPTION: Illustrates how 'typeof' can be applied to classes to get their
constructor type and to instances to get their object type. This helps in typing
variables that hold class constructors or instances.

SOURCE: [Link]
reference/[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
export class C {
foo: string;
}
export var c: C;
var c2: C;

export var r3: typeof C;


export var r4: typeof c;
export var r4b: typeof c2;
```

----------------------------------------

TITLE: TypeScript Greeter Class and Functions


DESCRIPTION: Demonstrates a TypeScript Greeter class with a constructor and a greet
method. Includes example functions `foo` and `foo2` to create and use Greeter
instances, showcasing class instantiation and array manipulation.

SOURCE: [Link]
reference/[Link]#_snippet_19
LANGUAGE: TypeScript
CODE:
```
"use strict";

class Greeter {
constructor(public greeting: string) {
}

greet() {
return "<h1>" + [Link] + "</h1>";
}
}

function foo(greeting: string): [Link] {


return new Greeter(greeting);
}

var greeter = new Greeter("Hello, world!");


var str = [Link]();

function foo2(greeting: string, ...restGreetings: string[]) {


var greeters: Greeter[] = [];
greeters[0] = new Greeter(greeting);
for (var i = 0; i < [Link]; i++) {
[Link](new Greeter(restGreetings[i]));
}

return greeters;
}

var b = foo2("Hello", "World", "!");


for (var j = 0; j < [Link]; j++) {
b[j].greet();
}
```

----------------------------------------

TITLE: Get TypeScript Compiler Version


DESCRIPTION: Retrieves the version of the installed TypeScript compiler. This
information is crucial for reporting issues.

SOURCE:
[Link]
28

LANGUAGE: npm
CODE:
```
npx tsc -v
```

LANGUAGE: yarn
CODE:
```
yarn tsc -v
```
----------------------------------------

TITLE: Install hereby Command Line Tool


DESCRIPTION: Installs the 'hereby' command-line tool globally, which is used for
building and testing changes within the TypeScript project.

SOURCE:
[Link]

LANGUAGE: bash
CODE:
```
npm install -g hereby
```

----------------------------------------

TITLE: TypeScript Module Example ([Link])


DESCRIPTION: Demonstrates basic TypeScript syntax including variable declaration,
class definition, instance creation, and function definition within a module.

SOURCE: [Link]
reference/project/maprootUrlSubfolderSpecifyOutputFile/node/
[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var m1_a1 = 10;
class m1_c1 {
public m1_c1_p1: number;
}

var m1_instance1 = new m1_c1();


function m1_f1() {
return m1_instance1;
}
```

----------------------------------------

TITLE: TypeScript Module Example ([Link])


DESCRIPTION: Demonstrates basic TypeScript syntax including variable declaration,
class definition, instance creation, and function definition within a module.

SOURCE: [Link]
reference/project/maprootUrlsourcerootUrlSubfolderSpecifyOutputFile/node/
[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var m1_a1 = 10;
class m1_c1 {
public m1_c1_p1: number;
}

var m1_instance1 = new m1_c1();


function m1_f1() {
return m1_instance1;
}
```

----------------------------------------

TITLE: TypeScript Module Example ([Link])


DESCRIPTION: Demonstrates basic TypeScript syntax including variable declaration,
class definition, instance creation, and function definition within a module.

SOURCE: [Link]
reference/project/sourcerootUrlSubfolderSpecifyOutputFile/node/
[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var m1_a1 = 10;
class m1_c1 {
public m1_c1_p1: number;
}

var m1_instance1 = new m1_c1();


function m1_f1() {
return m1_instance1;
}
```

----------------------------------------

TITLE: TypeScript Auto Imports Setup


DESCRIPTION: Illustrates the basic setup for auto-imports in a TypeScript file,
showing an import statement for a component.

SOURCE: [Link]
reference/[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
// === Auto Imports ===
// @Filename: /[Link]
import { Component } from "./[Link]";
/*|*/
```

----------------------------------------

TITLE: Remove Default TypeScript Props Import


DESCRIPTION: This snippet shows how to remove the default
[Link] import from a project file. It is necessary when
manually managing TypeScript integration.

SOURCE: [Link]
[Link]#_snippet_0

LANGUAGE: XML
CODE:
```
<Import
Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$
(VisualStudioVersion)\TypeScript\[Link]"
Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$
(VisualStudioVersion)\TypeScript\[Link]')" />
```

----------------------------------------

TITLE: Preact JSX Import Source Error Example


DESCRIPTION: Illustrates a Preact JSX import source setup that triggers a
TypeScript error (TS2875). The error indicates that the required module path
'preact/jsx-runtime' could not be found, suggesting a missing dependency or type
definition.

SOURCE: [Link]
reference/jsxJsxsCjsTransformKeyPropCustomImportPragma(jsx=react-
jsx).[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
/// <reference path="/.lib/[Link]" />
/* @jsxImportSource preact */
const props = { answer: 42 }
const a = <div key="foo" {...props}>text</div>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2875: This JSX tag requires the module path 'preact/jsx-runtime' to
exist, but none could be found. Make sure you have types for the appropriate
package installed.
const b = <div {...props} key="bar">text</div>;

export {};

```

----------------------------------------

TITLE: TypeScript Project Import Example


DESCRIPTION: An example of importing React and createStore from Redux within a
TypeScript file.

SOURCE: [Link]
reference/[Link]#_snippet_4

LANGUAGE: typescript
CODE:
```
import React from "react";
import { createStore } from "redux";
```

----------------------------------------

TITLE: Install and Use pprof-it Locally


DESCRIPTION: Instructions for installing pprof-it as a local dependency and using
it with npm scripts to profile TypeScript builds. This method allows integration
with existing build workflows.
SOURCE:
[Link]
33

LANGUAGE: sh
CODE:
```
npm install --no-save pprof-it
```

LANGUAGE: sh
CODE:
```
npm --node-option="--require pprof-it" run <your-script-name>
```

----------------------------------------

TITLE: TypeScript File /[Link] Example


DESCRIPTION: This is a simple TypeScript file containing a class definition. It is
presented as part of a project structure where compilation errors might occur.

SOURCE: [Link]
reference/
[Link]#_snippet_1

LANGUAGE: TypeScript
CODE:
```
class c {
}

```

----------------------------------------

TITLE: Remove Default TypeScript Targets Import


DESCRIPTION: This snippet demonstrates removing the default
[Link] import from a project file. This step is crucial for
custom TypeScript build configurations.

SOURCE: [Link]
[Link]#_snippet_1

LANGUAGE: XML
CODE:
```
<Import
Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$
(VisualStudioVersion)\TypeScript\[Link]"
Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$
(VisualStudioVersion)\TypeScript\[Link]')" />
```

----------------------------------------

TITLE: TypeScript Module Example ([Link])


DESCRIPTION: Demonstrates basic TypeScript syntax including variable declaration,
class definition, instance creation, and function definition within a module.

SOURCE: [Link]
reference/project/sourceRootAbsolutePathSubfolderSpecifyOutputFile/node/
[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var m1_a1 = 10;
class m1_c1 {
public m1_c1_p1: number;
}

var m1_instance1 = new m1_c1();


function m1_f1() {
return m1_instance1;
}
```

----------------------------------------

TITLE: TypeScript typeof on Enums


DESCRIPTION: Explains how to use the 'typeof' operator with enums to get the type
of the enum itself (which is an object at runtime) and its members.

SOURCE: [Link]
reference/[Link]#_snippet_4

LANGUAGE: typescript
CODE:
```
export enum E {
A
}
export var r10: typeof E;
export var r11: typeof E.A;
```

----------------------------------------

TITLE: TypeScript Module Example ([Link])


DESCRIPTION: Demonstrates basic TypeScript syntax including variable declaration,
class definition, instance creation, and function definition within a module.

SOURCE: [Link]
reference/project/mapRootAbsolutePathSubfolderSpecifyOutputFile/node/
[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var m1_a1 = 10;
class m1_c1 {
public m1_c1_p1: number;
}

var m1_instance1 = new m1_c1();


function m1_f1() {
return m1_instance1;
}
```

----------------------------------------

TITLE: Export TypeScript Class A as Namespace


DESCRIPTION: Exports all members from './a' as a namespace 'a'. This setup is part
of the chain leading to the 'import type' error.

SOURCE: [Link]
reference/[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
export * as a from './a';
```

----------------------------------------

TITLE: Compile TypeScript String to JavaScript


DESCRIPTION: Demonstrates how to create a TypeScript Program and compile code using
the Compiler API. It utilizes `createCompilerHost` for default file system
interactions and `createTypeChecker` to retrieve diagnostics.

SOURCE: [Link]
Compiler-API-(TypeScript-1.4).md#_snippet_1

LANGUAGE: typescript
CODE:
```
/// <reference path="typings/node/[Link]" />
/// <reference path="typings/typescript/[Link]" />

import ts = require("typescript");

export function compile(filenames: string[], options: [Link]): void {


var host = [Link](options);
var program = [Link](filenames, options, host);
var checker = [Link](program, /*produceDiagnostics*/ true);
var result = [Link]();

var allDiagnostics = [Link]()


.concat([Link]())
.concat([Link]);

[Link](diagnostic => {
var lineChar =
[Link]([Link]);
[Link](`${[Link]} (${[Link]},$
{[Link]}): ${[Link]}`);
});

[Link](`Process exiting with code '${[Link]}'.`);


[Link]([Link]);
}

compile([Link](2), { noEmitOnError: true, noImplicitAny: true,


target: [Link].ES5, module:
[Link] });
```

----------------------------------------

TITLE: Inspecting and Removing Docker Containers


DESCRIPTION: This section details how to list running Docker containers using
`docker ps` and how to forcefully remove a specific container using its ID with
`docker rm -f`. It's a common pattern for managing stray processes.

SOURCE: [Link]
[Link]#_snippet_13

LANGUAGE: shell
CODE:
```
docker ps
docker rm -f <container-id>
```

----------------------------------------

TITLE: Import fromBar from bar


DESCRIPTION: Imports 'fromBar' from an external package named 'bar'. This example
illustrates importing a named export from an installed package.

SOURCE: [Link]
reference/[Link]#_snippet_2

LANGUAGE: ts
CODE:
```
import { fromBar } from "bar";

fromBar
```

----------------------------------------

TITLE: TypeScript Module Example ([Link])


DESCRIPTION: Demonstrates basic TypeScript syntax including variable declaration,
class definition, instance creation, and function definition within a module.

SOURCE: [Link]
reference/project/outSubfolderSpecifyOutputFile/node/
[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var m1_a1 = 10;
class m1_c1 {
public m1_c1_p1: number;
}

var m1_instance1 = new m1_c1();


function m1_f1() {
return m1_instance1;
}
```

----------------------------------------

TITLE: TypeScript Configuration File


DESCRIPTION: The [Link] file specifies compiler options and files to include
in a TypeScript project. This example includes a single file '[Link]'.

SOURCE: [Link]
reference/[Link]#_snippet_0

LANGUAGE: json
CODE:
```
{ "files": "[Link]" }
```

----------------------------------------

TITLE: TypeScript Class Instantiation and Method Call


DESCRIPTION: Demonstrates the instantiation of a 'Greeter' class with a string
argument and calling its 'greet' method. This snippet showcases basic object-
oriented usage in TypeScript.

SOURCE: [Link]
reference/[Link]#_snippet_5

LANGUAGE: typescript
CODE:
```
class Greeter {
greeting: string;
constructor(message: string) {
[Link] = message;
}
greet() {
return "Hello, " + [Link];
}
}

var greeter = new Greeter("Hello, world!");


var str = [Link]();
```

----------------------------------------

TITLE: TypeScript Generator and Accessor Error Example


DESCRIPTION: This snippet demonstrates a TypeScript compilation error where a
'yield' expression is used within a 'get' accessor, which is not permitted. The
TypeScript compiler flags this as an error because 'get' accessors are not
generator functions and must return a value, not yield one. This highlights the
strict rules for generator functions and class accessors in TypeScript.

SOURCE: [Link]
reference/YieldExpression17_es6.[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var v = { get foo() { yield foo; } }
```

----------------------------------------

TITLE: TypeScript Module Definition Example


DESCRIPTION: Illustrates a basic TypeScript module declaration using 'export ='.
This pattern is used for exporting a single entity, such as a function, from a
module.

SOURCE: [Link]
reference/[Link]#_snippet_2

LANGUAGE: typescript
CODE:
```
declare function f(): string;
export = f;
```

----------------------------------------

TITLE: TypeScript Greeter Class and Functions


DESCRIPTION: This snippet demonstrates a TypeScript class 'Greeter' with a
constructor and a 'greet' method. It also includes utility functions 'foo' and
'foo2' for creating and managing Greeter instances. This code is intended to be
compiled into JavaScript, with source maps generated to link back to the original
TypeScript.

SOURCE: [Link]
reference/[Link]#_snippet_0

LANGUAGE: TypeScript
CODE:
```
namespace Foo {
export namespace Bar {
export class Greeter {
constructor(public greeting: string) {
}

greet() {
return "<h1>" + [Link] + "</h1>";
}
}

function foo(greeting: string): [Link] {


return new Greeter(greeting);
}

var greeter = new Greeter("Hello, world!");


var str = [Link]();

function foo2(greeting: string, ...restGreetings: string[]) {


var greeters: Greeter[] = [];
greeters[0] = new Greeter(greeting);
for (var i = 0; i < [Link]; i++) {
[Link](new Greeter(restGreetings[i]));
}

return greeters;
}

var b = foo2("Hello", "World", "!");


for (var j = 0; j < [Link]; j++) {
b[j].greet();
}
}
}
```

----------------------------------------

TITLE: TypeScript for loop with missing initialization


DESCRIPTION: Presents a 'for' loop in TypeScript where the initialization statement
is omitted. The loop variable must be declared and initialized before the loop
starts.

SOURCE: [Link]
reference/[Link]#_snippet_3

LANGUAGE: typescript
CODE:
```
for (; i < 10;) { // ok
i++;
}
```

LANGUAGE: typescript
CODE:
```
for (; i > 1; i--) { // ok
}
```

----------------------------------------

TITLE: TypeScript Module Example ([Link])


DESCRIPTION: Demonstrates basic TypeScript syntax including variable declaration,
class definition, instance creation, and function definition within a module.

SOURCE: [Link]
reference/project/sourceRootRelativePathSubfolderSpecifyOutputFile/node/
[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var m1_a1 = 10;
class m1_c1 {
public m1_c1_p1: number;
}

var m1_instance1 = new m1_c1();


function m1_f1() {
return m1_instance1;
}
```

----------------------------------------

TITLE: TypeScript Greeter Class and Usage


DESCRIPTION: Defines a Greeter class with a constructor and a greet method. It also
shows how to instantiate the class, call its method, and includes a helper function
within a namespace. The code demonstrates basic TypeScript syntax for classes,
methods, functions, and variables.

SOURCE: [Link]
reference/[Link]#_snippet_16

LANGUAGE: typescript
CODE:
```
"use strict";

class Greeter {
constructor(public greeting: string) {
}

greet() {
return "<h1>" + [Link] + "</h1>";
}
}

function foo(greeting: string): [Link] {


return new Greeter(greeting);
}

var greeter = new Greeter("Hello, world!");


var str = [Link]();

function foo2(greeting: string, ...restGreetings: string[]) {


var greeters: Greeter[] = [];
greeters[0] = new Greeter(greeting);
for (var i = 0; i < [Link]; i++) {
}
}
```

----------------------------------------

TITLE: TypeScript Getter Returning String


DESCRIPTION: A valid TypeScript `get` accessor example that successfully returns a
string value. This demonstrates a standard use case for getters.

SOURCE: [Link]
reference/[Link]#_snippet_12

LANGUAGE: typescript
CODE:
```
class C {
public get m3() {
return "Okay, because this is a return expression.";
}
}
```

----------------------------------------

TITLE: TypeScript: Accessing Bun File API


DESCRIPTION: This TypeScript code snippet demonstrates accessing the `[Link]()`
API to create a file object. It requires type definitions for Bun to resolve the
'Bun' global object. The provided example shows the usage that triggers a
TypeScript compilation error if types are not installed.

SOURCE: [Link]
reference/[Link]#_snippet_1

LANGUAGE: typescript
CODE:
```
const file = [Link]("/[Link]");

```

----------------------------------------

TITLE: TypeScript Test Compilation Context Setup


DESCRIPTION: Defines functions for pre-compilation and post-compilation steps,
along with a context object to manage these operations. This setup is typically
used within a testing harness for managing compilation units.

SOURCE: [Link]
reference/[Link]#_snippet_52

LANGUAGE: typescript
CODE:
```
var addedFiles = [];
var precompile = () => {
// REVIEW: if any dependency has a triple slash reference then does postCompile
potentially have to do a recreate since we can't update references with updateUnit?
// easy enough to do if so, prefer to avoid the recreate cost until it proves
to be an issue
[Link](dep => {
addUnit([Link], [Link], false,
[Link]([Link]));
[Link]([Link]);
});
};
var postcompile = () => {
[Link](file => {
updateUnit('', file);
});
};
var context = {
filename: filename,
preCompile: precompile,
postCompile: postcompile
};
return context;
```
----------------------------------------

TITLE: TypeScript Module Example ([Link])


DESCRIPTION: Demonstrates basic TypeScript syntax including variable declaration,
class definition, instance creation, and function definition within a module.

SOURCE: [Link]
reference/project/mapRootRelativePathSubfolderSpecifyOutputFile/node/
[Link]#_snippet_0

LANGUAGE: typescript
CODE:
```
var m1_a1 = 10;
class m1_c1 {
public m1_c1_p1: number;
}

var m1_instance1 = new m1_c1();


function m1_f1() {
return m1_instance1;
}
```

----------------------------------------

TITLE: TypeScript Module Import Example


DESCRIPTION: Demonstrates importing JavaScript and TypeScript modules in a
TypeScript file. It shows how to import default exports from different file types
using relative paths.

SOURCE: [Link]
reference/extensionLoadingPriority(moduleresolution=node16).[Link]#_snippet_3

LANGUAGE: typescript
CODE:
```
import a from "./[Link]";
import dir from "./dir";
```

----------------------------------------

TITLE: Generic Function with `get${T}` Mapped Type in TypeScript


DESCRIPTION: This example highlights a TypeScript error (TS2322) in a generic
function where a mapped type with a template literal key (`get${RemappedT}`) is
used to access a property. The error indicates that the retrieved value type is not
assignable to the generic type parameter `T` due to potential type mismatches.

SOURCE: [Link]
reference/[Link]#_snippet_3

LANGUAGE: typescript
CODE:
```
type Foo<T extends string> = {
[RemappedT in T as `get${RemappedT}`]: RemappedT;
};
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type
'Foo<T>[`get${T}`]' is not assignable to type 'T'

// Error details:
// [Link](82,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not
assignable to type 'T'.
// 'T' could be instantiated with an arbitrary type which could be unrelated to
'Foo<T>[`get${T}`]'.
```

----------------------------------------

TITLE: Declare Variable in TypeScript


DESCRIPTION: This snippet shows the original TypeScript code for declaring a
variable 'x' and assigning it the value '1'. It is the source code that gets
compiled.

SOURCE: [Link]
reference/[Link]#_snippet_0

LANGUAGE: TypeScript
CODE:
```
export var x = 1;
```

----------------------------------------

TITLE: Install Latest TypeScript Version


DESCRIPTION: Command to install the latest nightly build of TypeScript, useful for
testing if a bug has already been fixed.

SOURCE:
[Link]

LANGUAGE: bash
CODE:
```
npm install typescript@next
```

----------------------------------------

TITLE: Sample Project [Link]


DESCRIPTION: Defines the dependencies for a sample project used to test a local
TypeScript plugin. It includes a 'file:' dependency to link the plugin directly
from its source directory.

SOURCE: [Link]
[Link]#_snippet_9

LANGUAGE: json
CODE:
```
{
"name": "sample_project",
"dependencies": {
"your_plugin": "file:..",
"typescript": "^4.4.3"
}
}
```

----------------------------------------

TITLE: Basic TypeScript Hello World


DESCRIPTION: A fundamental TypeScript code snippet demonstrating a variable
declaration and assignment. This example is often used for initial setup and
testing of a TypeScript environment.

SOURCE: [Link]
reference/[Link]#_snippet_1

LANGUAGE: TypeScript
CODE:
```
var hello = "yo!";
[Link](hello);
```

----------------------------------------

TITLE: TypeScript Plugin: Full Example


DESCRIPTION: A comprehensive example of a TypeScript plugin that integrates
configuration from [Link] to filter completion entries and includes
diagnostic logging. It sets up a proxy for the Language Service and modifies
completion results.

SOURCE: [Link]
[Link]#_snippet_8

LANGUAGE: ts
CODE:
```
function init(modules: { typescript: typeof
import("typescript/lib/tsserverlibrary") }) {
const ts = [Link];

function create(info: [Link]) {


// Get a list of things to remove from the completion list from the config
object.
// If nothing was specified, we'll just remove 'caller'
const whatToRemove: string[] = [Link] || ["caller"];

// Diagnostic logging
[Link](
"I'm getting set up now! Check the log for this message."
);

// Set up decorator object


const proxy: [Link] = [Link](null);
for (let k of [Link]([Link]) as Array<keyof
[Link]>) {
const x = [Link][k]!;
// @ts-expect-error - JS runtime trickery which is tricky to type tersely
proxy[k] = (...args: Array<{}>) => [Link]([Link], args);
}
// Remove specified entries from completion list
[Link] = (fileName, position, options) => {
const prior = [Link](fileName,
position, options);
if (!prior) return

const oldLength = [Link];


[Link] = [Link](e => [Link]([Link]) < 0);

// Sample logging for diagnostic purposes


if (oldLength !== [Link]) {
const entriesRemoved = oldLength - [Link];
[Link](
`Removed ${entriesRemoved} entries from the completion list`
);
}

return prior;
};

return proxy;
}

return { create };
}

export = init;
```

----------------------------------------

TITLE: Basic TypeScript Hello World


DESCRIPTION: A fundamental TypeScript code snippet demonstrating a variable
declaration and assignment. This example is often used for initial setup and
testing of a TypeScript environment.

SOURCE: [Link]
reference/[Link]#_snippet_1

LANGUAGE: TypeScript
CODE:
```
var hello = "yo!";
[Link](hello);
```

----------------------------------------

TITLE: TypeScript Traditional for Loop


DESCRIPTION: Illustrates a traditional for loop iterating over a predefined array.
This example shows the setup for iterating through a fixed list of items using an
index.

SOURCE: [Link]
reference/
[Link]
#_snippet_10

LANGUAGE: TypeScript
CODE:
```
for (var _g = 0, _h = [robotA, robotB]; _g < _h.length; _g++) {
// Loop body would go here
}
```

----------------------------------------

TITLE: [Link] Constructor and Methods


DESCRIPTION: Demonstrates the [Link] constructor for creating locale objects
and accessing their properties. Includes examples of creating a Locale object and
using resolvedOptions.

SOURCE: [Link]
reference/[Link]#_snippet_4

LANGUAGE: APIDOC
CODE:
```
new [Link](tag?: string, options?: LocaleOptions)
Creates a new Locale object.
Parameters:
tag: A string representing a BCP 47 language tag.
options: An object with properties to customize the locale.

resolvedOptions(): ResolvedLocaleOptions
Returns an object with the canonicalized locale properties.

Example Usage:
// Creating a Locale object
const locale = new [Link]('en-US');
[Link]([Link]); // "en"
[Link]([Link]); // "US"

// Using resolvedOptions
const localesArg = ["es-ES", new [Link]("en-US")];
[Link]((new [Link](localesArg, {type:
'language'})).resolvedOptions().locale); // "es-ES"
```

You might also like