
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Table of contents:
npm install asserttt
asserttt
has no non-dev dependencies!
Type<Challenge[]>
.If a file contains type tests, it’s not enough to run it, we must also type-check it:
@ts-expect-error
annotation prevents the right kind of errorimport { type Assert, assertType, type Assignable, type Equal, type Extends, type Includes, type Not } from 'asserttt';
//========== Asserting types: Assert<B> ==========
{
type Pair<X> = [X, X];
type _1 = Assert<Equal<
Pair<'a'>, ['a', 'a']
>>;
type _2 = Assert<Not<Equal<
Pair<'a'>, ['x', 'x']
>>>;
}
{
type _ = [
Assert<Assignable<number, 123>>,
Assert<Extends<Array<string>, Object>>,
Assert<Not<Extends<Array<string>, RegExp>>>,
Assert<Includes<'a'|'b', 'a'>>,
Assert<Includes<'a'|'b'|'c', 'a'|'c'>>,
];
}
//========== Asserting types of values: assertType<T>(v) ==========
const n = 3 + 1;
assertType<number>(n);
Assert<B>
assertType<T>(value)
Equality:
Equal<X, Y>
MutuallyAssignable<X, Y>
PedanticEqual<X, Y>
Comparing/detecting types:
Extends<Sub, Super>
Assignable<Target, Source>
Includes<Superset, Subset>
IsAny<T>
Boolean operations:
Not<B>
MutuallyAssignable
export type MutuallyAssignable<X, Y> =
[X, Y] extends [Y, X] ? true : false
;
extends
prevent distributivity.any
is equal to all types – which is problematic when testing types.Equal
: like MutuallyAssignable
but any
is only equal to itselfThis Equal
predicate works well for many use cases:
type Equal<X, Y> =
[IsAny<X>, IsAny<Y>] extends [true, true] ? true
: [IsAny<X>, IsAny<Y>] extends [false, false] ? MutuallyAssignable<X, Y>
: false
;
type IsAny<T> = 0 extends (1 & T) ? true : false;
PedanticEqual
: a popular hack with several downsidestype PedanticEqual<X, Y> =
(<T>() => T extends X ? 1 : 2) extends // (A)
(<T>() => T extends Y ? 1 : 2) ? true : false // (B)
;
It was suggested by Matt McCutchen (source). How does it work (source)?
In order to check whether the function type in line A extends the function type in line B, TypeScript has to compare the following two conditional types:
T extends X ? 1 : 2
T extends Y ? 1 : 2
Since T
does not have a value, both conditional types are deferred. Assignability of two deferred conditional types is computed via the internal function isTypeIdenticalTo()
and only true
if:
Thanks to #1, X
and Y
are compared precisely.
This hack has several downsides: See test/pedantic-equal_test.ts
for more information.
type Assert<_T extends true> = void;
Alas, we can’t conditionally produce errors at the type level. That’s why we need to resort to a type parameter whose extends
constraint requires it to be assignable to true
.
(Idea by Blaine Bublitz)
Package ts-expect inspired this package. It’s very similar. This package uses different names and has a utility type Assert
(which doesn’t produce runtime code):
type _ = Assert<Equal<X,Y>>; // asserttt
expectType<TypeEqual<X, Y>>(true); // ts-expect
The type-challenges repository has a module with utility types for exercises. How is asserttt different?
Not
(vs. two versions of the same utility type).eslint-plugin-expect-type supports an elegant notation but requires a special tool (eslint) for checking.
FAQs
---
The npm package asserttt receives a total of 746 weekly downloads. As such, asserttt popularity was classified as not popular.
We found that asserttt demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.