Warning, /frameworks/syntax-highlighting/autotests/input/test.ts is written in an unsupported language. File is not indexed.

0001 /// <reference types="node" />
0002 
0003 // TypeScript Test
0004 
0005 class Student {
0006     fullName: string;
0007     constructor(public firstName: string, public middleInitial: string, public lastName: string) {
0008         this.fullName = firstName + " " + middleInitial + " " + lastName;
0009     }
0010 }
0011 
0012 interface Person {
0013     firstName: string;
0014     lastName: string;
0015 }
0016 
0017 function greeter(person : Person) {
0018     return "Hello, " + person.firstName + " " + person.lastName;
0019 }
0020 
0021 let user = new Student("Jane", "M.", "User");
0022 
0023 // Built-in
0024 document.body.innerHTML = greeter(user); to
0025 JSON.stringify()
0026 console.log("Hello world");
0027 const os = require('os');
0028 Math.LOG10E;
0029 Number.MAX_SAFE_INTEGER;
0030 String.raw`raw text \.\n${}`
0031 
0032 import http = require("http");
0033 import path = require("path");
0034 import URL = url.URL;
0035 import { Readable, Writable } from "stream";
0036 import { isBuffer, isString, isObject } from "util";
0037 
0038 const port = 8__88___8;
0039 const baseUrl = new URL(`http://localhost:${port}/`);
0040 const rootDir = path.dirname(__dirname);
0041 
0042 const defaultBrowser = os.platform() === "win32" ? "edge" : "chrome";
0043 let browser: "edge" | "chrome" | "none" = defaultBrowser;
0044 let grep: string | undefined;
0045 
0046 interface FileBasedTestConfiguration {
0047     [setting: string]: string;
0048 }
0049 
0050 function swapCase(s: string): string {
0051     return s.replace(/\w/g, (ch) => {
0052         const up = ch.toUpperCase();
0053         return ch === up ? ch.toLowerCase() : up;
0054     });
0055 }
0056 
0057 for (var i in pieces) {
0058     switch (true) {
0059         case /^\"?Accession\"?/.test(pieces[i]):
0060             numeration[0] = i;
0061             break;
0062     }
0063 }
0064 
0065 const enum ResponseCode {
0066     Success = 200,
0067     BadRequest = 400
0068 }
0069 
0070 // Substitutions
0071 export function isInOpenInterval(start: number, end: number) {
0072     return tersible(a => a > start && a < end, () => `(${start}...${end})`)
0073 }
0074 const bar = `${x} ${y}`;
0075 
0076 // readonly
0077 function f1(mt: [number, number], rt: readonly [number, number]) {
0078 }
0079 
0080 function f2(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) {
0081 }
0082 
0083 type ReadWrite<T> = { -readonly [P in keyof T] : T[P] };
0084 
0085 // const assertion
0086 let obj = { x: 10, y: [20, 30], z: { a: { b: 42 } } } as const;
0087 let r2 = { x: 20, y: 10 } as const;
0088 let r2 = {...p} as const;
0089 let p1 = { x: 10, y: 20 as const };
0090 let p2 = { 10 as const, 'foo' as const };
0091 
0092 // Definite assignment operator
0093 class Cl {
0094     one?: string;
0095     two!: string;
0096 }
0097 let x! = 1;
0098 
0099 // Function with multiple arguments
0100 const children = [].map<Set<Map<number, string>>>(element => {
0101     if (!this.identityProvider) {
0102         return element;
0103     }
0104     return element;
0105 });
0106 
0107 // Numerics
0108 var a = 0xA;
0109 var b = 0b1;
0110 var c = 0o7;
0111 var d = 1.1E+3;
0112 var e = 1.E+3;
0113 var f = .1E+3;
0114 var g = 1E+3;
0115 var h = 1.1;
0116 var i = 1.;
0117 var j = .1;
0118 var k =  1;
0119 var l = 1__.e+3_22 | .2____e2 | 0o1_23 | 11__. ;
0120 
0121 // Bigint
0122 const binBig = 0b101n;
0123 const octBig = 0o567n;
0124 const hexBig = 0xC0Bn;
0125 const decBig = 123n;
0126 
0127 // Types
0128 let a: null = null;
0129 let b: number = 123;
0130 let c: number = 123.456;
0131 let d: string = `Geeks`;
0132 let e: undefined = undefined;
0133 let f: boolean = true;
0134 let g: number = 0b111001; // Binary
0135 let h: number = 0o436; // Octal
0136 let i: number = 0xadf0d; // Hexa-Decimal
0137 let j: bigint = 123n;
0138 
0139 const query = query<[number], number>(`
0140         SELECT *
0141         FROM statistics
0142         WHERE unit_id = $1`)
0143 
0144 function runQuery() {
0145     const query = createQuery<[number[]], Table<Columns>>(`
0146         some SQL here
0147     `)
0148     return database.execute(query)
0149 }
0150 
0151 aa: <sdf/> string ?<ssd/> string
0152  string // Don't highlight
0153 aa: string assa |
0154    string
0155    string ;
0156    string
0157 
0158 aa: { string
0159     string } // Don't highlight
0160 
0161 aa: [ string
0162       string ]
0163 aa: ( string
0164       string ) // Don't highlight
0165 aa: string <string>
0166 interface a {
0167     aa: /* comment
0168         */ string,
0169     bb: /* comment */
0170         number,
0171     cc: // comment
0172         void,
0173     dd:
0174         any,
0175 }
0176 
0177 null, <{[key]: () => any}> null
0178 null, <{[key]: () =>{a: number}}> null
0179 
0180 // Correctly highlighting regular expressions
0181 dt = ((dt[0] * 1e9 + dt[1]) / 1e6).toFixed(3).replace(/\.?0+$/, '') + 'ms';
0182 (a[0] / 2) / (2)
0183 
0184 // Type guards
0185 function isNumber(x: any): x is number {
0186     return typeof x === "number";
0187 }
0188 
0189 // Conditional expressions (highlight keywords before ":")
0190 class C {
0191     w = () =>
0192         this.a() ? true : this.b() ? false : true;
0193     z = () =>
0194         this.b() ? hello : k;
0195 }
0196 
0197 function foo<T extends X>(arg: T): T extends B ? number : string {
0198   if (arg === "A") return <T extends B ? number : never>111;
0199   return <T extends B ? never : string>"returning a string";
0200 }
0201 
0202 let x = (foo === null || foo === undefined) ?
0203     undefined :
0204     foo.bar.baz();
0205 let y = (foo === null || foo === undefined) ? /* comment */ undefined :
0206     foo.bar.baz();
0207 
0208 // Operator ??
0209 let x = foo ?? bar();
0210 
0211 // Types and logical `&&` and `||` operators after `as` expression
0212 Date as any || null;
0213 
0214 //Assertions
0215 const assert: (value: unknown) => asserts value = value => {}
0216 declare function assertIsString(value: unknown): asserts value is string;
0217 declare function assertIsArrayOfStrings(value: unknown): asserts value is string[];
0218 declare function assertDefined<T>(value: T): asserts value is NonNullable<T>;
0219 namespace Debug {
0220     export declare function assert(value: unknown, message?: string): asserts value;
0221     export declare function assertDefined<T>(value: T): asserts value is NonNullable<T>;
0222 }
0223 
0224 // Only highlight valid regular expressions, of a single line, after strings
0225 // See: https://github.com/microsoft/TypeScript-TmLanguage/issues/786
0226 "text" /No-Regex
0227 "text" /Regex[:)]*/;
0228 const a = "6" / 2; /*comment*/ const b = 5;
0229 console.log("4" / "2"); // 2
0230 // Single quote
0231 const a = '6' / 2; /*comment*/ const b = 5;
0232 console.log('4' / '2'); // 2
0233 // Template
0234 const a = `6` / 2; /*comment*/ const b = 5;
0235 console.log(`4` / `2`); // 2
0236 
0237  [1, 2, 3].reduce<readonly number[]>((previous) => previous, []);
0238 
0239 // Multiline arrow assignment
0240 const h = {
0241   setSubProperty: <
0242       T extends A,
0243       K extends keyof T,
0244       J extends keyof T[K]
0245     >(
0246       property: K,
0247       key: J,
0248       value: T[K][J]
0249     ) =>  {}
0250 }
0251 
0252 // Private-Named Instance Fields
0253 class Greeter {
0254     #name: string;
0255     constructor(name: string) {
0256         this.#name = name;
0257     }
0258     greet() {
0259         console.log(`hello ${this.#name}`);
0260     }
0261 }
0262 class Test {
0263     #NAME1: () => string;
0264     prop1 = x.#NAMEabc.#NAMEabc;
0265     prop2 = x.#NAMEabc.#NAMEabc();
0266     prop3 = this.#NAMEabc.#NAME();
0267 }
0268 
0269 // Type-only imports and exports
0270 import type T from "mod";
0271 import type { U, V } from "mod";
0272 import type * as types from "mod";
0273 import type from "mod";
0274 import type = require("mod");
0275 import type T = require("mod");
0276 
0277 export { a as b, x };
0278 export * from "AnotherModule";
0279 export { k as m, l } from "AnotherModule";
0280 export type { T };
0281 export type { U } from "AnotherModule";
0282 
0283 // 'awaited' type operator: https://github.com/microsoft/TypeScript/pull/35998
0284 let v1: awaited number;
0285 let v2: awaited Promise<number>;
0286 function f7<U>() {
0287     let v0: awaited U;
0288     let v1: awaited Promise<U>;
0289 }
0290 
0291 // Tagged template literals
0292 tagFunc`
0293     Hello world!
0294     ${ alert("Hello!"); }`;
0295 obj.something.tagFunc`Setting ${setting} is ${value + 5}!`;