Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support integer or adding custom type #90

Open
Web-Engine opened this issue Feb 16, 2021 · 1 comment
Open

Feature: Support integer or adding custom type #90

Web-Engine opened this issue Feb 16, 2021 · 1 comment

Comments

@Web-Engine
Copy link

Web-Engine commented Feb 16, 2021

I want to check the value is a integer not number.

type integer = number;

interface Test {
  foo: integer;
  bar: string;
}

const isTest(obj: any): obj is Test => {
  if (!Number.isInteger(obj?.foo)) return false;
  if (typeof obj?.bar !== 'string') return false;

  return true;
}

const good = {
  foo: 1,
  bar: 'good',
};

const bad = {
  foo: 1.234,
  bar: 'bad',
};

console.log(isTest(good)); // true
console.log(isTest(bad)); // false

makes like:

import { is, integer } from 'typescript-is';

interface Test {
  foo: integer;
  bar: string;
}

const good = {
  foo: 1,
  bar: 'good',
};

const bad = {
  foo: 1.234,
  bar: 'bad',
};

console.log(is<Test>(good)); // true
console.log(is<Test>(bad)); // false

or

import { is, addType } from 'typescript-is';

type integer = number;
addType<integer>((obj: any): obj is integer => Number.isInteger(obj));

interface Test {
  foo: integer;
  bar: string;
}

const good = {
  foo: 1,
  bar: 'good',
};

const bad = {
  foo: 1.234,
  bar: 'bad',
};

console.log(is<Test>(good)); // true
console.log(is<Test>(bad)); // false
@Web-Engine Web-Engine changed the title Support integer or custom guard type Support integer or adding custom type Feb 16, 2021
@Web-Engine Web-Engine changed the title Support integer or adding custom type Feature: Support integer or adding custom type Feb 16, 2021
@samchon
Copy link
Contributor

samchon commented Oct 10, 2022

Possible by writing comment:

https://github.com/samchon/typescript-json#comment-tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants