npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@peerigon/typescript-exercises-tools

v2.0.0

Published

Tools for writing and validating TypeScript exercises

Downloads

25

Readme

@peerigon/typescript-exercises-tools

Tools for writing and validating TypeScript exercises.

Dependency Status Build Status Coverage Status Known Vulnerabilities

This module allows you to annotate expected type errors in your TypeScript code like this:

export const add = (a: number, b: string) => {
  return a + b;
};

// 💥 Expect error 2345: Argument of type '4' is not assignable to parameter of type...
add(3, 4);

With the TypeScript language service plugin provided by this module, these expected type errors will show up as a suggestion instead of an error:

Screenshot of an editor that shows a type error as a suggestion Screenshot of an editor that shows the suggestion popup

Furthermore, these comments will cause the editor to show an error if there is no type error at this location:

Screenshot of an editor that shows an error when there is no type error

At the time of writing, only editors use TypeScript language service plugins. Calling tsc from the command line will not execute the plugin. That's why there is also a programmatic API that allows you to do assertions on the program:

import { assertProgramToOnlyHaveExpectedErrors } from "@peerigon/typescript-exercises-tools/tests";

test("The program has only expected errors", () => {
  assertProgramToOnlyHaveExpectedErrors("/path/to/module.ts");
});

This module is ideal for:

  • testing types by doing assertions on the behavior of types
  • creating TypeScript exercises

Installation

npm install @peerigon/typescript-exercises-tools

Now you need to add the TypeScript language service plugin to your tsconfig.json:

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "@peerigon/typescript-exercises-tools"
      }
    ]
  }
}

Please note that you need to configure your editor to use your local TypeScript version. Otherwise TypeScript may not find the plugin. If you're using VSCode, create a file in your repository at .vscode/settings.json and add this:

{
  "typescript.tsdk": "node_modules/typescript/lib"
}

Support for multiple errors

If you expect more than one error, you can add multiple comments, like this:

type Person = {
  readonly name: string;
};

const person: Person = {
  name: "Bob",
};

// 💥 Expect error 2704: … cannot be a read-only property.
// 💥 Expect error 2790: … must be optional.
delete person.name;

API

assertProgramToOnlyHaveExpectedErrors(    programPath: string,    compilerOptions?: ts.CompilerOptions,): void

Compiles the program at the given programPath and throws the first unexpected error it encounters. It's recommended to use an absolute path. If you don't pass any compilerOptions to this function, it uses TypeScript's internal findConfigFile() to locate the closest tsconfig.json to programPath.

import { assertProgramToOnlyHaveExpectedErrors } from "@peerigon/typescript-exercises-tools/tests";

assertProgramToOnlyHaveExpectedErrors("/path/to/module.ts"); // may throw

assertProgramToOnlyHaveExpectedErrors(
  "/path/to/module.ts",
  { strict: true } // custom compiler options
);

License

Unlicense

Sponsors