| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -353,3 +353,36 @@ test('execute a TypeScript test mocking module', { skip: isWindows && process.ar | |||
| 353 | 353 | match(result.stdout, /Hello, TypeScript-CommonJS!/); | |
| 354 | 354 | strictEqual(result.code, 0); | |
| 355 | 355 | }); | |
| 356 | + | ||
| 357 | + test('execute a TypeScript file with union types', async () => { | ||
| 358 | + const result = await spawnPromisified(process.execPath, [ | ||
| 359 | + '--experimental-strip-types', | ||
| 360 | + '--no-warnings', | ||
| 361 | + fixtures.path('typescript/ts/test-union-types.ts'), | ||
| 362 | + ]); | ||
| 363 | + | ||
| 364 | + strictEqual(result.stderr, ''); | ||
| 365 | + strictEqual(result.stdout, | ||
| 366 | + '{' + | ||
| 367 | + " name: 'Hello, TypeScript!' }\n" + | ||
| 368 | + '{ role: \'admin\', permission: \'all\' }\n' + | ||
| 369 | + '{\n foo: \'Testing Partial Type\',\n bar: 42,\n' + | ||
| 370 | + ' zoo: true,\n metadata: undefined\n' + | ||
| 371 | + '}\n'); | ||
| 372 | + strictEqual(result.code, 0); | ||
| 373 | + }); | ||
| 374 | + | ||
| 375 | + test('expect error when executing a TypeScript file with generics', async () => { | ||
| 376 | + const result = await spawnPromisified(process.execPath, [ | ||
| 377 | + '--experimental-strip-types', | ||
| 378 | + fixtures.path('typescript/ts/test-parameter-properties.ts'), | ||
| 379 | + ]); | ||
| 380 | + | ||
| 381 | + // This error should be thrown during transformation | ||
| 382 | + match( | ||
| 383 | + result.stderr, | ||
| 384 | + /TypeScript parameter property is not supported in strip-only mode/ | ||
| 385 | + ); | ||
| 386 | + strictEqual(result.stdout, ''); | ||
| 387 | + strictEqual(result.code, 1); | ||
| 388 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 1 | + interface Bar { | ||
| 2 | + name: string; | ||
| 3 | + age: number; | ||
| 4 | + } | ||
| 5 | + | ||
| 6 | + class Test<T> { | ||
| 7 | + constructor(private value: T) {} | ||
| 8 | + | ||
| 9 | + public getValue(): T { | ||
| 10 | + return this.value; | ||
| 11 | + } | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + const foo = new Test<Bar>({ age: 42, name: 'John Doe' }); | ||
| 15 | + console.log(foo.getValue()); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,53 @@ | |||
| 1 | + // Use Some Union Types Together | ||
| 2 | + const getTypescript = async () => { | ||
| 3 | + return { | ||
| 4 | + name: 'Hello, TypeScript!', | ||
| 5 | + }; | ||
| 6 | + }; | ||
| 7 | + | ||
| 8 | + type MyNameResult = Awaited<ReturnType<typeof getTypescript>>; | ||
| 9 | + const myNameResult: MyNameResult = { | ||
| 10 | + name: 'Hello, TypeScript!', | ||
| 11 | + }; | ||
| 12 | + | ||
| 13 | + console.log(myNameResult); | ||
| 14 | + | ||
| 15 | + type RoleAttributes = | ||
| 16 | + | { | ||
| 17 | + role: 'admin'; | ||
| 18 | + permission: 'all'; | ||
| 19 | + } | ||
| 20 | + | { | ||
| 21 | + role: 'user'; | ||
| 22 | + } | ||
| 23 | + | { | ||
| 24 | + role: 'manager'; | ||
| 25 | + }; | ||
| 26 | + | ||
| 27 | + // Union Type: Extract | ||
| 28 | + type AdminRole = Extract<RoleAttributes, { role: 'admin' }>; | ||
| 29 | + const adminRole: AdminRole = { | ||
| 30 | + role: 'admin', | ||
| 31 | + permission: 'all', | ||
| 32 | + }; | ||
| 33 | + | ||
| 34 | + console.log(adminRole); | ||
| 35 | + | ||
| 36 | + type MyType = { | ||
| 37 | + foo: string; | ||
| 38 | + bar: number; | ||
| 39 | + zoo: boolean; | ||
| 40 | + metadata?: unknown; | ||
| 41 | + }; | ||
| 42 | + | ||
| 43 | + // Union Type: Partial | ||
| 44 | + type PartialType = Partial<MyType>; | ||
| 45 | + | ||
| 46 | + const PartialTypeWithValues: PartialType = { | ||
| 47 | + foo: 'Testing Partial Type', | ||
| 48 | + bar: 42, | ||
| 49 | + zoo: true, | ||
| 50 | + metadata: undefined, | ||
| 51 | + }; | ||
| 52 | + | ||
| 53 | + console.log(PartialTypeWithValues); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments