Comments (4)
@mohsen1 Yes it is 100% possible:
const a0: PositiveIntegerStringType<'0'> = 0; // works
const a1: PositiveIntegerStringType<'82739283293237'> = 82739283293237; // works
const a2: PositiveIntegerStringType<'82739.283293237'> = 82739.28329323; // never
const a3: PositiveIntegerStringType<'-82739.283293237'> = 82739.28329323; // never
const a4: PositiveIntegerStringType<'-1'> = -1; // never
When it works, no squiggly lines, when it's never you get the squiggly lines.
Here's the type definition:
type PositiveIntegerStringType<S extends string> = IfEquals<
IsPositiveInteger<Integer<NumerifyString<S>>>,
true,
Integer<NumerifyString<S>>,
never
>;
with
type Numeric = number | bigint;
/**
* Turn a given string literal to a numeric
* @example
*
* NumerifyString<'54'>; // 54
* NumerifyString<'699620.000000001'>; // 699620.000000001
* IsNegativeFloat<NumerifyString<'-699620.000000001'>>; // true
*/
export type NumerifyString<S extends string> = S extends `${infer N extends
Numeric}`
? N
: never;
/**
* Type representing an integer
*/
export type Integer<N extends Numeric> = IfExtends<
IsInteger<N>,
true,
N,
never
>;
/**
* Check if a given numeric value is an integer
* @returns
* true if it is, else false
*/
export type IsInteger<N extends Numeric> = number extends N
? false | true
: N extends N
? `${N}` extends `${string}.${string}`
? false
: true
: never;
/**
* Is it a positive integer ?
* @return
* `true` if it is, else `false`
*/
export type IsPositiveInteger<F extends Numeric> = IsPositive<Integer<F>>;
/**
* Checks if a given numeric value is in [0,+∞[
* @returns
* true if it is, otherwise false
*/
export type IsPositive<N extends Numeric> = N extends N
? Numeric extends N
? boolean
: `${N}` extends `-${Numeric}`
? false
: true
: never;
/**
* Conditional type that checks if type `T` is equal to type `P`.
* If `T` is equal to `P`, the type resolves to `Do`, otherwise `Else`.
* @example
* type Result1 = IfEquals<string, string, true, false>; // is true
* type Result2 = IfEquals<number, string, true, false>; // is false
* type Result3 = IfEquals<boolean, boolean, true, false>; // is true
*
* type IsExactlyString<T> = IfEquals<T, string, true, false>;
* type IsExactlyNumber<T> = IfEquals<T, number, true, false>;
*
* type TestString = IsExactlyString<string>; // is true
* type TestNumber = IsExactlyNumber<number>; // is false
* type TestBoolean = IsExactlyString<boolean>; // is false
*/
export type IfEquals<T, P, Do, Else> = Equals<T, P> extends true ? Do : Else;
/**
* Conditional type that checks if two types `X` and `Y` are exactly equal.
* If `X` is equal to `Y`, the type resolves to `true`; otherwise `false`.
* @example
* type Result1 = Equals<string, string>; // is true
* type Result2 = Equals<number, string>; // is false
* type Result3 = Equals<boolean | string, string | boolean>; // is true
*/
export type Equals<X, Y> = (<T>() => T extends X ? true : false) extends <
T,
>() => T extends Y ? true : false
? true
: false;
I hope that helps.
EDIT: I added it here
from type-fest.
Are you asking for "positive" or "non-negative"? Because 0
is not a positive number.
from type-fest.
We have some internal types here that may be useful as inspiration:
type-fest/source/internal.d.ts
Lines 113 to 147 in cb6cdab
type-fest/source/internal.d.ts
Lines 623 to 641 in cb6cdab
from type-fest.
Are you asking for "positive" or "non-negative"?
Concretely non-negative. Thank you for clarification and the hints in the source code.
I believe recursively inferring the first digit and ensuring it is on of 0..9 digits should work. Let me try this!
from type-fest.
Related Issues (20)
- `And` and `Or` is not exported at `index.d.ts`
- ConditionalKeys does not correctly filter out `never` type HOT 3
- Paths<T> should handle recursive types
- `PickDeep` strips `null` HOT 7
- `schema` should properly set type of objects in arrays HOT 3
- `SimplifyDeep` doesn't deep simplify lists HOT 3
- `IsStringLiteral` should be `false` for branded and tagged types
- `CamelCasedPropertiesDeep` doesn't handle `Tagged` type correctly HOT 1
- `IfxxxLiteral` (eg `IfBooleanLiteral`) are missing (only the `IsxxxLiteral` is present
- `PartialDeep` doesn't work with arrays HOT 3
- Document `IntRange` `Start` param must be `>=0`
- `Exact` does not work as expected with type involving `Date` and union HOT 2
- Web-specific version of `StructuredCloneable` type HOT 10
- Add `IsNullable` and `IsOptional`
- `LiteralList<Union>` HOT 2
- Exact not function correctly with Dates since last commit HOT 8
- Type `{}` is not assignable to type `PartialDeep<T>` HOT 1
- Expose Max Recursion Depth of Paths HOT 2
- Issues with `Paths` type HOT 4
- 4.23.0 introduced a breaking change in `Paths` type HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from type-fest.