Skip to content

two slash test

Published: at 03:47 AM

Table of contents

Open Table of contents

twoslash Tests

ts
// ts twoslash
console.log(str);
const str: "hello world"
Try
ts
// ts twoslash
console.log(str);
const str: "hello world"
Try
ts
// ts
const str = "hello world";
console.log(str);
ts
// ts
const str = "hello world";
console.log(str);

twoslash annotation

ts
function compact(arr) {
if (orr.length > 10) {
Cannot find name 'orr'.2304Cannot find name 'orr'.
return arr.trim(0, 10);
}
return arr;
}
Try

Discovered a typo, the param is arr, not orr!

ts
function compact(arr) {
if (orr.length > 10) {
Cannot find name 'orr'.2304Cannot find name 'orr'.
return arr.trim(0, 10);
}
return arr;
}
Try

Discovered a typo, the param is arr, not orr!

twoslash completions

ts
const arr = [1, 2];
 
function sum(...arr: number[]) {
return arr.s
              
}
Try
ts
const arr = [1, 2];
 
function sum(...arr: number[]) {
return arr.s
              
}
Try

twoslash error

ts
const a = "123";
a = 132;
Cannot assign to 'a' because it is a constant.2588Cannot assign to 'a' because it is a constant.
Try
ts
const a = "123";
a = 132;
Cannot assign to 'a' because it is a constant.2588Cannot assign to 'a' because it is a constant.
Try

twoslash filecut

ts
import { helloWorld } from "./a";
console.log(helloWorld);
Try
ts
import { helloWorld } from "./a";
console.log(helloWorld);
Try

twoslash json

ts
// @filename: app.json
{ "version": "23.2.3" }
 
// @filename: index.ts
import appSettings from "./app.json"
appSettings.version
(property) "version": string
Try
ts
// @filename: app.json
{ "version": "23.2.3" }
 
// @filename: index.ts
import appSettings from "./app.json"
appSettings.version
(property) "version": string
Try

twoslash log

ts
console.log("Hello world");
Hello world
 
console.warn("Ola Mundo");
Old Mundo
 
console.error("Neih hou");
Neih hou
Try
ts
console.log("Hello world");
Hello world
 
console.warn("Ola Mundo");
Old Mundo
 
console.error("Neih hou");
Neih hou
Try

twoslash twofiles

ts
// @filename: node_modules/@types/mylib/index.d.ts
export function doit(): string;
 
// @filename: index.ts
import { doit } from "mylib";
console.log(doit);
Try
ts
// @filename: node_modules/@types/mylib/index.d.ts
export function doit(): string;
 
// @filename: index.ts
import { doit } from "mylib";
console.log(doit);
Try

twoslash

ts
function createLabel<T extends number | string>(idOrName: T): NameOrId<T> {
throw "unimplemented";
}
 
let a = createLabel("typescript");
let a: NameLabel
 
let b = createLabel(2.8);
let b: IdLabel
 
let c = createLabel(Math.random() ? "hello" : 42);
let c: NameLabel | IdLabel
Try
ts
function createLabel<T extends number | string>(idOrName: T): NameOrId<T> {
throw "unimplemented";
}
 
let a = createLabel("typescript");
let a: NameLabel
 
let b = createLabel(2.8);
let b: IdLabel
 
let c = createLabel(Math.random() ? "hello" : 42);
let c: NameLabel | IdLabel
Try