Skip to content

Commit

Permalink
fix(type-compiler): target=es2022 now correctly loads TS libs
Browse files Browse the repository at this point in the history
fixes #339
  • Loading branch information
marcj committed Dec 3, 2022
1 parent 752c1db commit c8b92a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
10 changes: 8 additions & 2 deletions packages/type-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ import {
TypeReferenceNode,
UnionTypeNode,
visitEachChild,
visitNode, isVariableDeclaration, isTypeNode
visitNode, isVariableDeclaration, isTypeNode, ScriptTarget
} from 'typescript';
import {
ensureImportIsEmitted,
Expand Down Expand Up @@ -1679,7 +1679,13 @@ export class ReflectionTransformer implements CustomTransformer {

//todo also read compiler options "types" + typeRoot

const libs = knownLibFilesForCompilerOptions(this.context.getCompilerOptions(), ts);
//currently knownLibFilesForCompilerOptions from @typescript/vfs doesn't return correct lib files for ES2022,
//so we switch here to es2021 if bigger than es2021.
const options = {...this.context.getCompilerOptions()};
if (options.target && (options.target > ScriptTarget.ES2021 && options.target < ScriptTarget.ESNext)) {
options.target = ScriptTarget.ES2021;
}
const libs = knownLibFilesForCompilerOptions(options, ts);

for (const lib of libs) {
const sourceFile = this.resolver.resolveSourceFile(this.sourceFile.fileName, 'typescript/lib/' + lib.replace('.d.ts', ''));
Expand Down
35 changes: 18 additions & 17 deletions packages/type-compiler/tests/transpile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,24 @@ test('es2021', () => {
expect(res.app).toContain(`const type = typeOf([], [() => __ΩReadUser, 'n!'])`);
});

test('es2022', () => {
const res = transpile({
'app': `
interface User {
id: number;
name: string;
password: string;
}
type ReadUser = Omit<User, 'password'>;
const type = typeOf<ReadUser>();
`
}, {target: ts.ScriptTarget.ES2022});
console.log(res);
expect(res.app).toContain(`const __ΩPick = [`);
expect(res.app).toContain(`const type = typeOf([], [() => __ΩReadUser, 'n!'])`)
});


test('Return function ref', () => {
//see GitHub issue #354
const res = transpile({
Expand Down Expand Up @@ -408,20 +426,3 @@ test('extends with reference to this', () => {
//currently broken as it returns LogEntityForSchema.options.entity, probably a bug in TS
// expect(res.app).toContain(`() => this.options.entity,`);
});

//currently knownLibFilesForCompilerOptions from @typescript/vfs doesn't return correct lib files for ES2022
// test('es2022', () => {
// const res = transpile({
// 'app': `
// interface User {
// id: number;
// name: string;
// password: string;
// }
// type ReadUser = Omit<User, 'password'>;
// `
// }, {target: ts.ScriptTarget.ES2022});
// console.log(res);
// expect(res.app).toContain(`const __ΩPick = [`);
// expect(res.app).toContain(`const type = typeOf([], [() => __ΩReadUser, 'n!'])`)
// });

0 comments on commit c8b92a2

Please sign in to comment.