Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support typescript 5.0 new syntax #859

Merged
merged 1 commit into from Mar 19, 2023
Merged

feat: support typescript 5.0 new syntax #859

merged 1 commit into from Mar 19, 2023

Conversation

SuperchupuDev
Copy link
Contributor

@SuperchupuDev SuperchupuDev commented Mar 18, 2023

Adds support for TS 5.0's new syntax, part of #858
This PR doesn't add support for mentioning multiple files in extends, so #849 wouldn't get fixed by this

Testing

export function loggedMethod(originalMethod: any, _context: any) {

  function replacementMethod(this: any, ...args: any[]) {
      console.log("LOG: Entering method.")
      const result = originalMethod.call(this, ...args);
      console.log("LOG: Exiting method.")
      return result;
  }

  return replacementMethod;
}

export class Person {
  name: string;
  constructor(name: string) {
      this.name = name;
  }

  @loggedMethod
  greet() {
      console.log(`Hello, my name is ${this.name}.`);
  }
}

const p = new Person("Ray");
p.greet();

export type HasNames = { readonly names: string[] };
export function getNamesExactly<const T extends HasNames>(arg: T): T["names"] {
    return arg.names;
}

// Inferred type: readonly ["Alice", "Bob", "Eve"]
export const names = getNamesExactly({ names: ["Alice", "Bob", "Eve"]});

gets converted to

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result)
    __defProp(target, key, result);
  return result;
};

// testlo.ts
var testlo_exports = {};
__export(testlo_exports, {
  Person: () => Person,
  getNamesExactly: () => getNamesExactly,
  loggedMethod: () => loggedMethod,
  names: () => names
});
module.exports = __toCommonJS(testlo_exports);
function loggedMethod(originalMethod, _context) {
  function replacementMethod(...args) {
    console.log("LOG: Entering method.");
    const result = originalMethod.call(this, ...args);
    console.log("LOG: Exiting method.");
    return result;
  }
  return replacementMethod;
}
var Person = class {
  name;
  constructor(name) {
    this.name = name;
  }
  greet() {
    console.log(`Hello, my name is ${this.name}.`);
  }
};
__decorateClass([
  loggedMethod
], Person.prototype, "greet", 1);
var p = new Person("Ray");
p.greet();
function getNamesExactly(arg) {
  return arg.names;
}
var names = getNamesExactly({ names: ["Alice", "Bob", "Eve"] });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Person,
  getNamesExactly,
  loggedMethod,
  names
});

with the following d.ts:

declare function loggedMethod(originalMethod: any, _context: any): (this: any, ...args: any[]) => any;
declare class Person {
    name: string;
    constructor(name: string);
    greet(): void;
}
type HasNames = {
    readonly names: string[];
};
declare function getNamesExactly<const T extends HasNames>(arg: T): T["names"];
declare const names: string[];

export { HasNames, Person, getNamesExactly, loggedMethod, names };

export type * also works but it added a lot of types that would make this section harder to read

@codesandbox
Copy link

codesandbox bot commented Mar 18, 2023

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@vercel
Copy link

vercel bot commented Mar 18, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
tsup ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 18, 2023 at 4:26PM (UTC)

"vitest": "0.28.4",
"wait-for-expect": "3.0.2"
},
"peerDependencies": {
"@swc/core": "^1",
"postcss": "^8.4.12",
"typescript": "^4.1.0"
"typescript": ">=4.1.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not be something more like ^4.1.0 || ^5.0.0 since a newer version could break this again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in typescript, new minor versions, i.e 4.2 to 4.3 have the same potential breaking changes than major versions, in fact TS 5.0 does not break tsup at all if you don't use some of the new features

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unfortunately true (see microsoft/TypeScript#14116). If TypeScript followed semantic versioning, we would have major version bumps every single release. It is likely that we'd be at TypeScript 50+ by now! To Ryan Cavanaugh, the frequent major version bumps of TypeScript hence effectively render the semver scheme meaningless.

@egoist egoist merged commit 3e15867 into egoist:dev Mar 19, 2023
8 checks passed
@github-actions
Copy link

🎉 This PR is included in version 6.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants