jsr-core / unknownutil Goto Github PK
View Code? Open in Web Editor NEW๐ฆ A lightweight utility pack for handling unknown type
Home Page: https://jsr.io/@core/unknownutil
License: MIT License
๐ฆ A lightweight utility pack for handling unknown type
Home Page: https://jsr.io/@core/unknownutil
License: MIT License
SSIA
We are going to support only JSR.
We have introduced a new metadata feature, providing us with the opportunity to implement improved error messages.
SSIA
Here is a test code.
import { assert, is } from "https://deno.land/x/[email protected]/mod.ts";
const a: unknown = {
constructor: {
x: 1,
},
};
assert(a, is.RecordOf(is.ObjectOf({ x: is.Number })));
Taking into account the potential for future advancements in Deno runtime, including tree shaking support, and various other factors, we initially exposed functions of the isXXXXX
type. However, with the requirement for cross-platform functionality driven by JSR support, the feasibility of tree shaking has been reduced. Offering the same function in two manners only adds to complexity and confusion in this context, leading us to opt for its removal.
This action mitigates the risk of unintended exposure of functions or types through export * from "..."
. Furthermore, it allows us to eliminate tests aimed at verifying if is
encompasses all exposed isXXXXX
functions.
To create subpredicate functions from an existing one. For example, assume you can use the following code:
const isArticle = is.ObjectOf({
id: is.String,
subject: is.String,
body: is.String,
});
const isArticleCreateParams = is.OmitOf(isArticle, ["id"]);
// Equivalent to
//const isArticleCreateParams = is.ObjectOf({
// subject: is.String,
// body: is.String,
//});
I'm uncertain about whether OmitOf
and PickOf
make sense. I'd like to maintain the *Of
convention, but if it seems unclear, perhaps we should consider naming them OmitFrom
and PickFrom
instead, or explore other alternatives.
The predicate function should narrowed to Foo | Bar
but it actually narrowed to Foo
in the following case
type Predicate<T> = (x: unknown) => x is T;
type Foo = {
foo: string;
};
type Bar = {
foo: string;
bar?: string;
};
type F = Predicate<Foo>;
type B = Predicate<Bar>;
// Please fix this definition.
type OneOf<T extends Predicate<unknown>[]> = T extends Predicate<infer U>[] ? U : never;
const a: OneOf<[F, B]> = {foo: "", bar: ""}; // `a` is not `Foo | Bar` but `Foo` so this assignment fails.
I'm not sure if this is TypeScript limitation but you can play this with TS Playground.
I want to define following object.
import { is } from "https://deno.land/x/[email protected]/mod.ts";
export const isResponse = is.IntersectionOf([
is.ObjectOf({
id: is.String,
}),
is.UnionOf([
is.ObjectOf({
result: is.String,
}),
is.ObjectOf({
error: is.String,
}),
]),
]);
but occurs this error.
error: TS2345 [ERROR]: Argument of type '[Predicate<{ id: string; }> & WithMetadata<IsObjectOfMetadata>, Predicate<{ result: string; } | { error: string; }> & WithMetadata<...>]' is not assignable to parameter of type 'readonly [Predicate<unknown> & WithMetadata<IsObjectOfMetadata>, ...(Predicate<unknown> & WithMetadata<IsObjectOfMetadata>)[]]'.
Type at position 1 in source is not compatible with type at position 1 in target.
Type 'Predicate<{ result: string; } | { error: string; }> & WithMetadata<IsUnionOfMetadata>' is not assignable to type 'Predicate<unknown> & WithMetadata<IsObjectOfMetadata>'.
Type 'Predicate<{ result: string; } | { error: string; }> & WithMetadata<IsUnionOfMetadata>' is not assignable to type 'WithMetadata<IsObjectOfMetadata>'.
The types of '[metadataKey].name' are incompatible between these types.
Type '"isUnionOf"' is not assignable to type '"isObjectOf"'.
export const isResponse = is.IntersectionOf([
TypeScript offers the Readonly<T>
type function and the readonly
keyword. However, the isReadonlyOf
function seems a bit ambiguous. I propose the following improvements:
isReadonlyOf
for Array, Tuple, and Object types.isReadonlyFieldOf
for checking/annotating the readonly
status of object fields.isOptionalOf
to isOptionalFieldOf
to maintain consistency with isReadonlyFieldOf
. This change aligns with the handling of ?
for object fields.Like
@core/unknownutil/is-any
@core/unknownutil/is-unknown
So that user can import minimum requirements. This change may cause the recommended style change (e.g. prefer isXXXXX
over is.XXXXX
)
Here is the reproduce steps:
mkdir ex && cd ex
bun init -y # bun creates tsconfig.json, so I use this
npm i # install with node
npx jsr add @core/[email protected]
# or
# bun i
# bunx jsr add @core/[email protected]
Then, Open index.ts
and write import
statement.
And you can see all methods are any
However, if you use named import, it works
This is my environmental information:
/t/example 3m 45.9s โฑ node --version
v22.0.0
/t/example โฑ uname -mprs
Darwin 23.5.0 arm64 arm
I checked with
vtsls
+ nvim-lspconfig
but the results are same.
SSIA
Probably I would rename as
isRecord
to isRecordObject
isRecordOf
to isRecordObjectOf
isRecordLike
to isRecord
isRecordLikeOf
to isRecordOf
Because current isRecord/isRecordOf
is more specific than isRecordLike/isRecordLikeOf
and I think specific version should have longer name.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.