Giter Club home page Giter Club logo

Comments (4)

AshGw avatar AshGw commented on June 21, 2024 1

@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.

sindresorhus avatar sindresorhus commented on June 21, 2024

Are you asking for "positive" or "non-negative"? Because 0 is not a positive number.

from type-fest.

sindresorhus avatar sindresorhus commented on June 21, 2024

We have some internal types here that may be useful as inspiration:

  • /**
    Converts a numeric string to a number.
    @example
    ```
    type PositiveInt = StringToNumber<'1234'>;
    //=> 1234
    type NegativeInt = StringToNumber<'-1234'>;
    //=> -1234
    type PositiveFloat = StringToNumber<'1234.56'>;
    //=> 1234.56
    type NegativeFloat = StringToNumber<'-1234.56'>;
    //=> -1234.56
    type PositiveInfinity = StringToNumber<'Infinity'>;
    //=> Infinity
    type NegativeInfinity = StringToNumber<'-Infinity'>;
    //=> -Infinity
    ```
    @category String
    @category Numeric
    @category Template literal
    */
    export type StringToNumber<S extends string> = S extends `${infer N extends number}`
    ? N
    : S extends 'Infinity'
    ? PositiveInfinity
    : S extends '-Infinity'
    ? NegativeInfinity
    : never;
  • /**
    Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters.
    @example
    ```
    PositiveNumericCharacterGt<'5', '1'>;
    //=> true
    PositiveNumericCharacterGt<'1', '1'>;
    //=> false
    ```
    */
    type PositiveNumericCharacterGt<A extends string, B extends string> = NumericString extends `${infer HeadA}${A}${infer TailA}`
    ? NumericString extends `${infer HeadB}${B}${infer TailB}`
    ? HeadA extends `${HeadB}${infer _}${infer __}`
    ? true
    : false
    : never
    : never;

from type-fest.

mohsen1 avatar mohsen1 commented on June 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.