tsdjs / tsd

Check TypeScript type definitions
MIT License
2.36k stars 68 forks source link

Allow testing string sources #181

Open tommy-mitchell opened 1 year ago

tommy-mitchell commented 1 year ago

Closes #86.

Allows testing files passed to tsd as strings, either programmatically:

import tsd from 'tsd';

const diagnostics = await tsd({
    testFiles: [
        'file1.ts',
        {
            'name': 'file2.ts',
            'text': 'import {expectType} from \'tsd\';\nexpectType<number>(1);',
        },
    ],
});

or via the CLI with a concise syntax (file.extension:source):

$ npx tsd --files "foo.ts:$(cat output.txt)"

Type definitions have been updated like so:

type TestFiles = ReadonlyArray<(
    | string
    | {name: string; text: string}
)>;

type SourceFiles = ReadonlyArray<{name: string; text: string}>;

type ParsedTestFiles = {
    globs: readonly string[];
    sourceFiles?: SourceFiles;
};

interface Context {
    cwd: string;
    pkg: PackageJsonWithTsdConfig;
    typingsFile: string;
    testFiles: ParsedTestFiles;
    config: Config;
}

interface Options {
    cwd: string;
    typingsFile?: string;
    testFiles?: TestFiles;
}

Still some items to finish, but opening this PR to get feedback:

sindresorhus commented 1 year ago

Just a warning. I have very little time to spend on this project. So I will not be able to review non-essential things. I'm just helping out to keep it up to date with TS releases. So if you plan to make a lot of changes, I would recommend reaching out to @SamVerschueren to become a maintainer.