unional / type-plus Goto Github PK
View Code? Open in Web Editor NEWAdditional types and types adjusted utilities for TypeScript
Home Page: https://unional.github.io/type-plus/
License: MIT License
Additional types and types adjusted utilities for TypeScript
Home Page: https://unional.github.io/type-plus/
License: MIT License
To handle all special types in options and result.
What if user want to return the branches directly?
That means we need special wrap types for each special types for this purposes, but they can still be treated as special type branches inside the code.
for testing:
type R = number | undefined
testType.hasUndefined<R>(true)
TS now has Awaited<T>
It appears to be quite easy to extend the implementation of GreaterThan
to also support negative numbers, which would make it significantly more useful.
Here's my rough implementation, including the fix from #161.
1.1.3
to 1.1.4
.π¨ View failing branch.
This version is covered by your current version range and after updating it in your project the build failed.
@unional/devpkg-node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those donβt help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot π΄
To remove:
First
(FindFirst
, ArrayPlus.Find
)isType.t()
(isType()
, testType.true()
)isType.f()
(isType()
, testType.false()
)isType.never()
(isType()
, testType.never()
)isType.equal()
(testType.equal()
)CommonKeys
(CommonPropKeys
)PadLeft
(PadStart
)Newly deprecated:
drop()
(no replacement)Concat
(ArrayPlus.Concat
)testType.canAssign<string|number, number>(true)
testType.canAssign<string|number, number>(false)
I don't know if it's by design, but both IsTuple
and IsArray
return false
for never[]
in the latest version of type-plus
.
TypeScript might have do something special for X<T, Then, Else>
inference.
Need to investigate this
Currently this is not possible because TypeScript does not support infer generic declaration:
const newType = {
toNative: <T>(value: T) => Generate<T['value']>
}
function createChecker(...newTypes: Specs) {
return {
satisfy<T>(type: T, subject: unknown): Generate<Specs, T> { ... }
}
}
Inside Generate<>
, it cannot pass the T
to the toNative()
when we found which type can handles T
.
the type checking types will support various options like distribute
, exact
.
Need to see how to support these options without breaking the inference.
Also, may add IsXXX<T, $O, $Else = never>
for backward compatibility before completely dropping the <T, Then, Else>
support.
NumericStruct.Add<NumericStruct.FromNumeric<A, Fail>, NumericStruct.FromNumeric<B, Fail>>
// ^ fail as input not accepting `Fail`
Instead of:
type NumericStruct.Add<A extends NumericStruct, B extends NumericStruct> ...
Accept Maybe<NumericStruct>
and propagate the error.
This will simplify many implementation.
Update to support Options: { caseNever, caseArray, caseEmptyTuple, caseUpperBound, caseLowerBound }
The type definition of the infer type generated by TypeScript has error.
Making consumer of this package need to do skipLibCheck
to get pass compilation.
currently, testType.*
can be used in test directly. e.g.:
it('blah', () => {
testType.equal<A, B>(true)
}
However, when the tests get complicates or when there are may duplication, you would like to extract part of the test and reuse it:
it('blah', () => {
testMyType(...)
}
it('bruh', () => {
testMyType(...)
}
testMyType(...) {
testType....
}
Since testType.*
are π₯ immediate
, they can't be used this way.
Not sure if there is a way to achieve this a the library level as this is crossing the type-value boundary (i.e. fail type throws Error
)
each builder.with()
should be isolated from each other,
so that it can do this:
const b = stub.builder(...)
const x = b.with(...)
const y = b.with(...)
const a = x.create()
const b = y.create()
Equal<A, B, T = A, E = never>
NotEqual<A, B, T = A, E = never>
Changing Equal<>
is not a problem and is clear, as A
and B
are equal, returning either one is fine.
NotEqual<>
is more problematic as It's ambiguous on which one to return.
Returning A
per "convention" helps, but the logic is not clean.
There are probably use cases for this, as the filter
variant is more generic than the predicate
variant.
Will keep it as is for now, until there are sufficient use cases to support the change.
This will be a breaking change.
Forgive me if I have understood wrong (the functionality) but the following code shouldn't work without compiler problems?
assertType.isTrue(true as CanAssign<any, number>);
If the order is inverted, it works fine. Also, the following code apparently compiles:
let a: any = 4;
let b: number = a;
let d: number = 3;
let e: any = d;
Thank you :)
Something like:
import { pluginA } from 'plugin-a'
import { pluginB } from 'plugin-b'
const checker = T.createChecker(pluginA, pluginB)
const subject: unknown = { ... }
checker.check(pluginA.create(...), subject)
The plugin needs to supply a type guard function, and the type.
It might not be possible as generic on generic is not possible.
like ArrayPlus.Find
etc.
type-plus:build: src/mix_types/merge.spec.ts(71,6): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'never'.
type-plus:build: src/string/is_not_string_literal.spec.ts(305,77): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_not_string_literal.spec.ts(306,98): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_not_template_literal.spec.ts(102,64): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_not_template_literal.spec.ts(106,71): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_not_template_literal.spec.ts(194,90): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_not_template_literal.spec.ts(198,97): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_string_literal.spec.ts(632,73): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_string_literal.spec.ts(740,95): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(213,62): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(215,58): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(216,61): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(219,67): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(225,69): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(228,69): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(412,88): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(414,84): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(415,87): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(418,93): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(424,95): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type-plus:build: src/string/is_template_literal.spec.ts(427,95): error TS2345: Argument of type 'true' is not assignable to parameter of type 'false'.
type A = {
a?: number,
b?: string
}
type X = LeftJoin<A, { b: boolean }> // { a: number, b: boolean }
type Foo = { a?: string, b: string }
const koo: Pick<Foo, 'a'> = {} // error, koo: { a: string | undefined }
Not sure if there is a way to detect the optional flag of the property.
/cc @dragomirtitian to see if you have an idea.
2.2.2
to 2.2.3
.π¨ View failing branch.
This version is covered by your current version range and after updating it in your project the build failed.
@size-limit/preset-small-lib is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those donβt help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot π΄
Currently T.union.create(T.boolean)
will get { type, value, any, true, false, create, optional, ... }
from T.boolean
.
It would be great if we can simplify it to only { type, value }
as other type information are just noise.
We can do it for one value:
Value
-> Pick<Value, 'type' | 'value'>
Need a way to map Pick
over array and objects.
I wasn't aware that
testType
had acanAssign
(the documentation apparently didn't mention it yet).
Um, thanks for pointing it out. I thought I have the doc updated. Seems like I have missed it.
Originally posted by @unional in #349 (comment)
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
.github/workflows/codeql-analysis.yml
actions/checkout v4
github/codeql-action v2
github/codeql-action v2
github/codeql-action v2
.github/workflows/pull-request.yml
.github/workflows/release.yml
package.json
@biomejs/biome ^1.6.3
@repobuddy/biome ^1.3.0
@changesets/cli ^2.25.2
@commitlint/cli ^19.0.0
@commitlint/config-conventional ^19.0.0
eslint ^8.52.0
eslint-plugin-yml ~1.13.0
husky ^9.0.11
jest ^29.7.0
jest-watch-suspend ^1.1.2
jest-watch-toggle-config-2 ^2.1.0
jest-watch-typeahead ^2.2.2
npm-check-updates ^16.0.0
npm-run-all2 ^6.0.0
rimraf ^5.0.5
ts-jest ^29.1.2
turbo 1.13.0
typedoc ^0.25.0
typedoc-plugin-extras ^3.0.0
typescript ~5.4.2
pnpm 8.15.4
packages/kind/package.json
@repobuddy/typescript ^2.0.0
depcheck ^1.4.3
npm-run-all2 ^6.0.0
rimraf ^5.0.5
packages/type-plus/package.json
tersify ^3.11.1
unpartial ^1.0.4
@jest/globals ^29.5.0
@repobuddy/jest ^4.0.0
@repobuddy/typescript ^2.0.0
@size-limit/esbuild-why ^9.0.0
@size-limit/preset-small-lib ^9.0.0
@types/node ^18.0.0
@unional/fixture ^3.0.0
assertron ^11.0.1
cross-env ^7.0.3
depcheck ^1.4.3
jest ^29.7.0
jest-watch-suspend ^1.1.2
jest-watch-toggle-config-2 ^2.1.0
jest-watch-typeahead ^2.2.2
npm-run-all2 ^6.0.0
rimraf ^5.0.5
satisfier ^5.2.2
size-limit ^9.0.0
ts-jest ^29.1.2
pages/package.json
@astrojs/check ^0.5.10
@astrojs/solid-js ^4.0.0
@astrojs/starlight ^0.21.3
@astrojs/tailwind ^5.1.0
astro ^4.5.9
sharp ^0.33.3
solid-js ^1.4.3
tailwindcss ^3.4.3
7.0.2
to 7.1.0
.π¨ View failing branch.
This version is covered by your current version range and after updating it in your project the build failed.
assertron is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those donβt help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot π΄
The filter
vs validate
types are useful only if not modified.
Also it promotes bad usage as the If<Condition, Then, Else>
enclosed type logic can lead to infinite result due to the lack of actual conditional type support.
It's better to return ThenType
and ElseType
and then Foo<...> extends infer R? R extends ThenType ? ... : ...
etc.
The syntax and implementation still needs some clean up.
But the general direction should be good.
All types should follow Foo<...inputs, Options>: R
signature.
I devised a fast logarithmic string length algorithm based on carefully building string type patterns which is surprisingly tedious due to ${string}${string}
simplifying to ${string}
so we need to add an extra character $
before and after during all of our checks.
Anyways, this string length algorithm supports getting the length of strings up to 10,000 characters long (uses tuples for math). However, you could add an alternate path after the cache gets to 2^13 (8,192) to use your arbitrary math type to support more strings up until a very large size due to beauty of the logarithmic scale.
type TCache = [string, 0[]];
type StringLengthUp<
T extends string,
$Acc extends 0[] = [],
$Cache extends TCache[] = [[`$${string}`, [0]]],
> =
//
$Cache extends [infer C extends TCache, ...infer $RestCache extends TCache[]]
? (
`${C[0]}${C[0]}_` extends `$${string}$${infer $After}`
? `${C[0]}${$After}` extends `${infer $Before}_` ? $Before : never
: never
) extends infer $DoubleC extends string
? `$${T}` extends `${$DoubleC}${infer $Rest}` ? StringLengthUp<
$Rest,
[...$Acc, ...C[1], ...C[1]],
[[$DoubleC, [...C[1], ...C[1]]], ...$Cache]
>
: `$${T}` extends `${C[0]}${infer $Rest}`
? StringLengthUp<$Rest, [...$Acc, ...C[1]], $Cache>
: StringLengthDown<T, $Acc, $RestCache>
: never
: $Acc["length"];
type StringLengthDown<
T extends string,
$Acc extends 0[],
$Cache extends TCache[],
> = $Cache extends
[infer C extends TCache, ...infer $RestCache extends TCache[]]
? `$${T}` extends `${C[0]}${infer $Rest}`
? StringLengthDown<$Rest, [...$Acc, ...C[1]], $Cache>
: StringLengthDown<T, $Acc, $RestCache>
: $Acc["length"];
type StringLength<T extends string> = T extends "" ? 0 : StringLengthUp<T>;
type String9999 = "<a 9999 character long string>";
type String9999Length = StringLength<String9999>; // 9999
The code is quite ugly so there still might be some opportunities for simplification.
And the docs for IsNegative
is incorrect.
testType.equal<() => void, (o?: number) => void>(true) // should be false
Here is another implementation of Equal
that almost work.
Keeping it here for reference:
BothNever<
A,
B,
Then,
Else,
BothAny<
A,
B,
Then,
Else,
And<
IsSymbol<A>,
IsSymbol<B>,
Then,
And<
IsObject<A>,
IsObject<B>,
And<
IsFunction<A>,
IsFunction<B>,
[A, B] extends [B, A] ? Then : Else,
Properties<A> extends infer PA
? Properties<B> extends infer PB
? (<_>() => _ extends (PA & _) | _ ? 1 : 2) extends <_>() => _ extends (PB & _) | _ ? 1 : 2
? [PA, PB] extends [A, B]
? Then
: Else
: Else
: never
: never
>,
[A, B] extends [B, A] ? Then : Else
>
>
>
>
24.0.1
to 24.0.2
.π¨ View failing branch.
This version is covered by your current version range and after updating it in your project the build failed.
@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those donβt help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot π΄
test('Just<string> is not assignable to Maybe<number>', () => {
assertType.isFalse(false as CanAssign<Maybe<number>, Just<'abc'>>)
// this is failing, which is wrong
assertType.isFalse(canAssign<Maybe<number>>()(Just('abc')))
})
Added StrictArrayType
for the current behavior.
The ArrayType
should accept both array and tuple like StringType
accept string and string literals.
Added LooseArrayType
for the time being, before the breaking change.
There seems to be an issue with the GreaterThan
implementation, so it is wrong for certain combinations of numbers (but none of the numbers tested in the .spec.ts file).
Example (TS Playground):
import { GreaterThan } from "type-plus";
type test1 = GreaterThan<10, 19> // Correct (false)
type test2 = GreaterThan<19, 10> // Incorrect (returns false but should be true)
The issue seems to be in the implementation of OnDigitArray
, which as far as I can tell should just look like this:
type OnDigitArray<DA extends number[], DB extends number[]> = (
Equal<Head<DA>, Head<DB>> extends true
? OnDigitArray<Tail<DA>, Tail<DB>>
: Digit.GreaterThan<Head<DA>, Head<DB>>
)
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.