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_2.e+3_22 | .2e2 | 0o1_23 | 11.2 | 08;
0120 
0121 // Bigint
0122 const binBig = 0b101n;
0123 const octBig = 0o567n;
0124 const hexBig = 0xC0Bn;
0125 const decBig = 123n;
0126 
0127 // Invalid numbers
0128 var l = 0xA1t;
0129 var m = 0b0123;
0130 var n = 0o29;
0131 var n = 0_7;
0132 var n = 1___1;
0133 var n = 1__;
0134 var n = 11__.;
0135 var n = 1__.e+3_22;
0136 var n = .2_e2;
0137 
0138 // Types
0139 let a: null = null;
0140 let b: number = 123;
0141 let c: number = 123.456;
0142 let d: string = `Geeks`;
0143 let e: undefined = undefined;
0144 let f: boolean = true;
0145 let g: number = 0b111001; // Binary
0146 let h: number = 0o436; // Octal
0147 let i: number = 0xadf0d; // Hexa-Decimal
0148 let j: bigint = 123n;
0149 
0150 const query = query<[number], number>(`
0151         SELECT *
0152         FROM statistics
0153         WHERE unit_id = $1`)
0154 
0155 function runQuery() {
0156     const query = createQuery<[number[]], Table<Columns>>(`
0157         some SQL here
0158     `)
0159     return database.execute(query)
0160 }
0161 
0162 aa: <sdf/> string ?<ssd/> string
0163  string // Don't highlight
0164 aa: string assa |
0165    string
0166    string ;
0167    string
0168 
0169 aa: { string
0170     string } // Don't highlight
0171 
0172 aa: [ string
0173       string ]
0174 aa: ( string
0175       string ) // Don't highlight
0176 aa: string <string>
0177 interface a {
0178     aa: /* comment
0179         */ string,
0180     bb: /* comment */
0181         number,
0182     cc: // comment
0183         void,
0184     dd:
0185         any,
0186 }
0187 
0188 null, <{[key]: () => any}> null
0189 null, <{[key]: () =>{a: number}}> null
0190 
0191 // Correctly highlighting regular expressions
0192 dt = ((dt[0] * 1e9 + dt[1]) / 1e6).toFixed(3).replace(/\.?0+$/, '') + 'ms';
0193 (a[0] / 2) / (2)
0194 
0195 // Type guards
0196 function isNumber(x: any): x is number {
0197     return typeof x === "number";
0198 }
0199 
0200 // Conditional expressions (highlight keywords before ":")
0201 class C {
0202     w = () =>
0203         this.a() ? true : this.b() ? false : true;
0204     z = () =>
0205         this.b() ? hello : k;
0206 }
0207 
0208 function foo<T extends X>(arg: T): T extends B ? number : string {
0209   if (arg === "A") return <T extends B ? number : never>111;
0210   return <T extends B ? never : string>"returning a string";
0211 }
0212 
0213 let x = (foo === null || foo === undefined) ?
0214     undefined :
0215     foo.bar.baz();
0216 let y = (foo === null || foo === undefined) ? /* comment */ undefined :
0217     foo.bar.baz();
0218 
0219 // Operator ??
0220 let x = foo ?? bar();
0221 
0222 // Types and logical `&&` and `||` operators after `as` expression
0223 Date as any || null;
0224 
0225 //Assertions
0226 const assert: (value: unknown) => asserts value = value => {}
0227 declare function assertIsString(value: unknown): asserts value is string;
0228 declare function assertIsArrayOfStrings(value: unknown): asserts value is string[];
0229 declare function assertDefined<T>(value: T): asserts value is NonNullable<T>;
0230 namespace Debug {
0231     export declare function assert(value: unknown, message?: string): asserts value;
0232     export declare function assertDefined<T>(value: T): asserts value is NonNullable<T>;
0233 }
0234 
0235 // Only highlight valid regular expressions, of a single line, after strings
0236 // See: https://github.com/microsoft/TypeScript-TmLanguage/issues/786
0237 "text" /No-Regex
0238 "text" /Regex[:)]*/;
0239 const a = "6" / 2; /*comment*/ const b = 5;
0240 console.log("4" / "2"); // 2
0241 // Single quote
0242 const a = '6' / 2; /*comment*/ const b = 5;
0243 console.log('4' / '2'); // 2
0244 // Template
0245 const a = `6` / 2; /*comment*/ const b = 5;
0246 console.log(`4` / `2`); // 2
0247 
0248  [1, 2, 3].reduce<readonly number[]>((previous) => previous, []);
0249 
0250 // Multiline arrow assignment
0251 const h = {
0252   setSubProperty: <
0253       T extends A,
0254       K extends keyof T,
0255       J extends keyof T[K]
0256     >(
0257       property: K,
0258       key: J,
0259       value: T[K][J]
0260     ) =>  {}
0261 }
0262 
0263 // Private-Named Instance Fields
0264 class Greeter {
0265     #name: string;
0266     constructor(name: string) {
0267         this.#name = name;
0268     }
0269     greet() {
0270         console.log(`hello ${this.#name}`);
0271     }
0272 }
0273 class Test {
0274     #NAME1: () => string;
0275     prop1 = x.#NAMEabc.#NAMEabc;
0276     prop2 = x.#NAMEabc.#NAMEabc();
0277     prop3 = this.#NAMEabc.#NAME();
0278 }
0279 
0280 // Type-only imports and exports
0281 import type T from "mod";
0282 import type { U, V } from "mod";
0283 import type * as types from "mod";
0284 import type from "mod";
0285 import type = require("mod");
0286 import type T = require("mod");
0287 
0288 export { a as b, x };
0289 export * from "AnotherModule";
0290 export { k as m, l } from "AnotherModule";
0291 export type { T };
0292 export type { U } from "AnotherModule";
0293 
0294 // 'awaited' type operator: https://github.com/microsoft/TypeScript/pull/35998
0295 let v1: awaited number;
0296 let v2: awaited Promise<number>;
0297 function f7<U>() {
0298     let v0: awaited U;
0299     let v1: awaited Promise<U>;
0300 }
0301 
0302 // Tagged template literals
0303 tagFunc`
0304     Hello world!
0305     ${ alert("Hello!"); }`;
0306 obj.something.tagFunc`Setting ${setting} is ${value + 5}!`;