New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tsd-lite

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsd-lite - npm Package Compare versions

Comparing version 0.7.0 to 0.8.0

42

build/assertions.d.ts
/**
* Check that the type of `value` is identical to type `T`.
*
* @param value - Value that should be identical to type `T`.
* Asserts that the type of `expression` is assignable to type `T`.
*/
export declare function expectType<T>(value: unknown): void;
export declare function expectAssignable<T>(expression: unknown): void;
/**
* Check that the type of `value` is not identical to type `T`.
*
* @param value - Value that should be identical to type `T`.
* Asserts that the type of `expression` is not assignable to type `T`.
*/
export declare function expectNotType<T>(value: unknown): void;
export declare function expectNotAssignable<T>(expression: unknown): void;
/**
* Check that the type of `value` is assignable to type `T`.
*
* @param value - Value that should be assignable to type `T`.
* Asserts that the type of `expression` is identical to type `T`.
*/
export declare function expectAssignable<T>(value: unknown): void;
export declare function expectType<T>(expression: unknown): void;
/**
* Check that the type of `value` is not assignable to type `T`.
*
* @param value - Value that should not be assignable to type `T`.
* Asserts that the type of `expression` is not identical to type `T`.
*/
export declare function expectNotAssignable<T>(value: unknown): void;
export declare function expectNotType<T>(expression: unknown): void;
/**
* Assert the value to throw an argument error.
*
* @param value - Value that should be checked.
* Asserts the `expression` has a type error.
*/
export declare function expectError<T = unknown>(value: T): void;
/**
* Assert that the `expression` provided is marked as `@deprecated`.
*
* @param expression - Expression that should be marked as `@deprecated`.
*/
export declare function expectDeprecated(expression: unknown): void;
/**
* Assert that the `expression` provided is not marked as `@deprecated`.
*
* @param expression - Expression that should not be marked as `@deprecated`.
*/
export declare function expectNotDeprecated(expression: unknown): void;
export declare function expectError(value: unknown): void;

@@ -8,5 +8,3 @@ import type * as ts from "@tsd/typescript";

EXPECT_ASSIGNABLE = "expectAssignable",
EXPECT_NOT_ASSIGNABLE = "expectNotAssignable",
EXPECT_DEPRECATED = "expectDeprecated",
EXPECT_NOT_DEPRECATED = "expectNotDeprecated"
EXPECT_NOT_ASSIGNABLE = "expectNotAssignable"
}

@@ -13,0 +11,0 @@ export type Handler = (typeChecker: ts.TypeChecker, nodes: Set<ts.CallExpression>) => Array<AssertionResult>;

@@ -5,3 +5,2 @@ "use strict";

const assignable_1 = require("./assignable");
const deprecated_1 = require("./deprecated");
const identical_1 = require("./identical");

@@ -15,5 +14,3 @@ var Assertion;

Assertion["EXPECT_NOT_ASSIGNABLE"] = "expectNotAssignable";
Assertion["EXPECT_DEPRECATED"] = "expectDeprecated";
Assertion["EXPECT_NOT_DEPRECATED"] = "expectNotDeprecated";
})(Assertion = exports.Assertion || (exports.Assertion = {}));
})(Assertion || (exports.Assertion = Assertion = {}));
const assertionHandlers = new Map([

@@ -24,4 +21,2 @@ [Assertion.EXPECT_TYPE, identical_1.expectType],

[Assertion.EXPECT_NOT_ASSIGNABLE, assignable_1.expectNotAssignable],
[Assertion.EXPECT_DEPRECATED, deprecated_1.expectDeprecated],
[Assertion.EXPECT_NOT_DEPRECATED, deprecated_1.expectNotDeprecated],
]);

@@ -28,0 +23,0 @@ function handleAssertions(typeChecker, assertions) {

export { tsdLite as default } from "./tsdLite";
export { expectAssignable, expectDeprecated, expectError, expectNotAssignable, expectNotDeprecated, expectNotType, expectType, } from "./assertions";
export { expectAssignable, expectError, expectNotAssignable, expectNotType, expectType, } from "./assertions";
export type { TsdResult } from "./types";
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.expectType = exports.expectNotType = exports.expectNotDeprecated = exports.expectNotAssignable = exports.expectError = exports.expectDeprecated = exports.expectAssignable = exports.default = void 0;
exports.expectType = exports.expectNotType = exports.expectNotAssignable = exports.expectError = exports.expectAssignable = exports.default = void 0;
var tsdLite_1 = require("./tsdLite");

@@ -8,7 +8,5 @@ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return tsdLite_1.tsdLite; } });

Object.defineProperty(exports, "expectAssignable", { enumerable: true, get: function () { return assertions_1.expectAssignable; } });
Object.defineProperty(exports, "expectDeprecated", { enumerable: true, get: function () { return assertions_1.expectDeprecated; } });
Object.defineProperty(exports, "expectError", { enumerable: true, get: function () { return assertions_1.expectError; } });
Object.defineProperty(exports, "expectNotAssignable", { enumerable: true, get: function () { return assertions_1.expectNotAssignable; } });
Object.defineProperty(exports, "expectNotDeprecated", { enumerable: true, get: function () { return assertions_1.expectNotDeprecated; } });
Object.defineProperty(exports, "expectNotType", { enumerable: true, get: function () { return assertions_1.expectNotType; } });
Object.defineProperty(exports, "expectType", { enumerable: true, get: function () { return assertions_1.expectType; } });
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseErrorAssertionToLocation = exports.extractAssertions = void 0;
const ts = require("@tsd/typescript");
const ts = __importStar(require("@tsd/typescript"));
const handleAssertions_1 = require("./handleAssertions");
const utils_1 = require("./utils");
const assertionFnNames = new Set(Object.values(handleAssertions_1.Assertion));

@@ -10,3 +34,12 @@ function extractAssertions(program) {

function visit(node) {
var _a;
if (ts.isImportDeclaration(node) &&
/^("|')tsd("|')$/.test(node.moduleSpecifier.getText())) {
throw new utils_1.TsdError("Usage Error", {
messageText: "The assertions must be imported from 'tsd-lite' package, please refactor the type test. " +
"This is a precaution to prevent bugs and errors caused by differences between the testing APIs. " +
"You should also consider uninstalling 'tsd' to reduce the number of redundant dependencies.",
file: node.moduleSpecifier.getSourceFile(),
start: node.moduleSpecifier.getStart(),
});
}
if (ts.isCallExpression(node)) {

@@ -16,3 +49,3 @@ const identifier = node.expression.getText();

const assertion = identifier;
const nodes = (_a = assertions.get(assertion)) !== null && _a !== void 0 ? _a : new Set();
const nodes = assertions.get(assertion) ?? new Set();
nodes.add(node);

@@ -19,0 +52,0 @@ assertions.set(assertion, nodes);

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.silenceError = void 0;
const ts = require("@tsd/typescript");
const ts = __importStar(require("@tsd/typescript"));
// For reference see:

@@ -6,0 +29,0 @@ // https://github.com/microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tsdLite = void 0;
const ts = require("@tsd/typescript");
const ts = __importStar(require("@tsd/typescript"));
const handleAssertions_1 = require("./handleAssertions");

@@ -21,7 +44,7 @@ const parser_1 = require("./parser");

if (syntacticDiagnostics.length !== 0) {
throw new utils_1.TsdError(syntacticDiagnostics[0], "SyntaxError");
throw new utils_1.TsdError("SyntaxError", syntacticDiagnostics[0]);
}
const semanticDiagnostics = program.getSemanticDiagnostics();
const { assertions, assertionsCount } = (0, parser_1.extractAssertions)(program);
const typeChecker = program.getTypeChecker();
const { assertions, assertionsCount } = (0, parser_1.extractAssertions)(program);
const assertionResults = (0, handleAssertions_1.handleAssertions)(typeChecker, assertions);

@@ -28,0 +51,0 @@ const expectedErrors = (0, parser_1.parseErrorAssertionToLocation)(assertions);

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveCompilerOptions = void 0;
const path_1 = require("path");
const ts = require("@tsd/typescript");
const ts = __importStar(require("@tsd/typescript"));
const TsdError_1 = require("./TsdError");

@@ -15,3 +38,3 @@ function resolveCompilerOptions(searchPath) {

if (configDiagnostics.length > 0) {
throw new TsdError_1.TsdError(configDiagnostics[0], "ConfigError");
throw new TsdError_1.TsdError("ConfigError", configDiagnostics[0]);
}

@@ -18,0 +41,0 @@ return compilerOptions;

import * as ts from "@tsd/typescript";
export interface TsdErrorOptions {
file?: ts.SourceFile | undefined;
messageText: string | ts.DiagnosticMessageChain;
start?: number | undefined;
}
export declare class TsdError extends Error {
constructor(diagnostic: ts.Diagnostic, name: string);
constructor(name: string, options: TsdErrorOptions);
}
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TsdError = void 0;
const ts = require("@tsd/typescript");
const isDiagnosticWithLocation_1 = require("./isDiagnosticWithLocation");
function formatMassageAndLocation(diagnostic) {
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
if ((0, isDiagnosticWithLocation_1.isDiagnosticWithLocation)(diagnostic)) {
const { file, start } = diagnostic;
const { line, character } = file.getLineAndCharacterOfPosition(start);
const location = `at ${file.fileName}:${line + 1}:${character + 1}`;
return { message, location };
}
return { message };
}
const ts = __importStar(require("@tsd/typescript"));
class TsdError extends Error {
constructor(diagnostic, name) {
const { message, location } = formatMassageAndLocation(diagnostic);
constructor(name, options) {
let location;
if (options.file != null && options.start != null) {
const { line, character } = options.file.getLineAndCharacterOfPosition(options.start);
location = `at ${options.file.fileName}:${line + 1}:${character + 1}`;
}
const message = ts.flattenDiagnosticMessageText(options.messageText, "\n");
super(message);

@@ -20,0 +37,0 @@ this.name = name;

{
"name": "tsd-lite",
"version": "0.7.0",
"version": "0.8.0",
"description": "Test your TypeScript types easily",

@@ -19,12 +19,12 @@ "keywords": [

},
"repository": {
"type": "git",
"url": "git+https://github.com/mrazauskas/tsd-lite.git"
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
"build/**/*"
],
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/mrazauskas/tsd-lite.git"
},
"scripts": {

@@ -40,16 +40,18 @@ "build": "tsc",

"devDependencies": {
"@babel/core": "7.21.0",
"@babel/preset-env": "7.20.2",
"@babel/preset-typescript": "7.21.0",
"@jest/globals": "29.4.3",
"@tsd/typescript": "4.9.5",
"@typescript-eslint/eslint-plugin": "5.54.1",
"@typescript-eslint/parser": "5.54.1",
"babel-jest": "29.4.3",
"cspell": "6.28.0",
"eslint": "8.35.0",
"eslint-config-prettier": "8.7.0",
"jest": "29.4.3",
"prettier": "2.8.4",
"typescript": "4.9.5"
"@babel/core": "7.22.5",
"@babel/preset-env": "7.22.5",
"@babel/preset-typescript": "7.22.5",
"@jest/globals": "29.5.0",
"@tsconfig/node16": "1.0.4",
"@tsd/typescript": "5.1.3",
"@types/node": "20.3.1",
"@typescript-eslint/eslint-plugin": "5.60.0",
"@typescript-eslint/parser": "5.60.0",
"babel-jest": "29.5.0",
"cspell": "6.31.1",
"eslint": "8.43.0",
"eslint-config-prettier": "8.8.0",
"jest": "29.5.0",
"prettier": "2.8.8",
"typescript": "5.1.3"
},

@@ -59,6 +61,6 @@ "peerDependencies": {

},
"packageManager": "yarn@3.6.0",
"engines": {
"node": ">=14"
},
"packageManager": "yarn@3.4.1"
"node": ">=16"
}
}

@@ -11,4 +11,2 @@ # tsd-lite

> **Note** This library is intended for programmatic use only. For an integration with Jest see [`jest-runner-tsd`](https://github.com/jest-community/jest-runner-tsd), if you prefer standalone CLI implementation check [`tsd-lite-cli`](https://github.com/asd-xiv/tsd-lite-cli).
## Motivation

@@ -21,3 +19,3 @@

- `tsd-lite` performs only type testing without any additional checks or rules.
- Exposes only general type related assertions: `expectAssignable`, `expectDeprecated`, `expectType` and their counterparts. Currently other APIs (like `expectNever`, `expectDocCommentIncludes` and `printType`) are not implement.
- Exposes only general type related assertions: `expectAssignable`, `expectNotAssignable`, `expectError`, `expectType` and `expectNotType`. All other APIs (like `expectNever`, `expectDeprecated`, `expectDocCommentIncludes` and `printType`) are not implement.
- Comes with no default compiler options.

@@ -27,3 +25,3 @@ - Reads TypeScript compiler options from the nearest `tsconfig.json` for each test file (does not read options from `package.json`).

- [`@tsd/typescript`](https://npmjs.com/package/@tsd/typescript) package is moved to peer dependencies.
- `tsd-lite` allows only programmatic [usage](#usage).
- `tsd-lite` allows only programmatic usage. For an integration with Jest see [`jest-runner-tsd`](https://github.com/jest-community/jest-runner-tsd), if you prefer standalone CLI implementation check [`tsd-lite-cli`](https://github.com/asd-xiv/tsd-lite-cli).

@@ -40,5 +38,99 @@ ## Install

## Usage
## Assertions
The library provides the following type testing assertions.
### expectAssignable&lt;T&gt;(expression)
Asserts that the type of `expression` is assignable to type `T`.
### expectNotAssignable&lt;T&gt;(expression)
Asserts that the type of `expression` is not assignable to type `T`.
```ts
// JsonObject.ts
type JsonValue = string | number | boolean | JsonObject | Array<JsonValue>;
export interface JsonObject {
[key: string]: JsonValue;
}
```
```ts
// __typetests__/JsonObject.test.ts
import { expectAssignable, expectNotAssignable } from "tsd-lite";
import type { JsonObject } from "../JsonObject.js";
expectAssignable<JsonObject>({
caption: "test",
count: 100,
isTest: true,
location: { name: "test", start: [1, 2], valid: false, x: 10, y: 20 },
values: [0, 10, 20, { x: 1, y: 2 }, true, "test", ["a", "b"]],
});
expectNotAssignable<JsonObject>({
filter: () => {},
});
```
### expectType&lt;T&gt;(expression)
Asserts that the type of `expression` is identical to type `T`.
### expectNotType&lt;T&gt;(expression)
Asserts that the type of `expression` is not identical to type `T`.
```ts
// MethodLikeKeys.ts
type FunctionLike = (...args: any) => any;
export type MethodLikeKeys<T> = keyof {
[K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];
};
```
```ts
// __typetests__/MethodLikeKeys.test.ts
import { expectType, expectNotType } from "tsd-lite";
import type { MethodLikeKeys } from "../MethodLikeKeys.js";
interface FixtureInterface {
methodA?: ((a: boolean) => void) | undefined;
methodB: (b: string) => boolean;
propertyA?: number | undefined;
propertyB?: number;
propertyC: number | undefined;
propertyD: string;
}
declare const interfaceMethods: MethodLikeKeys<FixtureInterface>;
expectType<"methodA" | "methodB">(interfaceMethods);
expectNotType<"methodA" | "methodB" | "propertyA">(interfaceMethods);
```
### expectError(expression)
Asserts the `expression` has a type error.
```ts
// __typetests__/require-resolve.test.ts
import { expectError, expectType } from "tsd-lite";
// Expected 1-2 arguments
expectError(require.resolve());
// Returns a value of type 'string'
expectType<string>(require.resolve("tsd-lite"));
```
## API Reference
The default export of the library is a function which takes fully resolved path to a test file as an argument:
```ts
import tsdLite from "tsd-lite";

@@ -51,8 +143,4 @@

## API Reference
It returns an object with `assertionsCount` and `tsdResults` properties:
### `tsdLite(testFilePath: string)`
The exported function takes fully resolved path to a test file as an argument and returns an object:
```ts

@@ -69,3 +157,3 @@ {

`tsd-lite` will throw if TS compiler encountered an error while parsing `tsconfig.json` or a syntax error is found while compiling the code.
`tsd-lite` will throw if the TypeScript compiler encounters an error while parsing `tsconfig.json` or finds a syntax error in the code.

@@ -72,0 +160,0 @@ ## License

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc