GithubHelp home page GithubHelp logo

isabella232 / typescript-exercises-tools Goto Github PK

View Code? Open in Web Editor NEW

This project forked from peerigon/typescript-exercises-tools

0.0 0.0 0.0 687 KB

Tools for writing and validating TypeScript exercises

License: The Unlicense

TypeScript 98.60% JavaScript 1.40%

typescript-exercises-tools's Introduction

@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";

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/plugin"
        }]
    }
}

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"
}

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";

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

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

License

Unlicense

Sponsors

typescript-exercises-tools's People

Contributors

jhnns avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.