Skip to content

Commit ce67e7b

Browse files
kevinueharatargos
authored andcommitted
test: adding more tests for strip-types
PR-URL: #54929 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent 286ea4e commit ce67e7b

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

test/es-module/test-typescript.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,36 @@ test('execute a TypeScript test mocking module', { skip: isWindows && process.ar
353353
match(result.stdout, /Hello, TypeScript-CommonJS!/);
354354
strictEqual(result.code, 0);
355355
});
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+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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());
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy