Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

deleted 2 characters in body
Source Link
Calvintwr
  • 8.8k
  • 5
  • 33
  • 48

A clean approach

You can consider using a very small, dependency-free library like Issable. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
is(number).number() // returns true
is(array).number() // throws error
// so you need to define your own:
import { define } from 'issable'
// or require syntax
const { define } = require('issable')
define({
 primitives: 'number',
 nameOfTyping: 'integer',
 toPass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
is(4.4).custom('integer') // throws error
is(8).customercustom('integer') // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 is(string).string()
 is(boolean).boolean()
 // continue with your code.
}

A clean approach

You can consider using a very small, dependency-free library like Issable. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
is(number).number() // returns true
is(array).number() // throws error
// so you need to define your own:
import { define } from 'issable'
// or require syntax
const { define } = require('issable')
define({
 primitives: 'number',
 nameOfTyping: 'integer',
 toPass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
is(4.4).custom('integer') // throws error
is(8).customer('integer') // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 is(string).string()
 is(boolean).boolean()
 // continue with your code.
}

A clean approach

You can consider using a very small, dependency-free library like Issable. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
is(number).number() // returns true
is(array).number() // throws error
// so you need to define your own:
import { define } from 'issable'
// or require syntax
const { define } = require('issable')
define({
 primitives: 'number',
 nameOfTyping: 'integer',
 toPass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
is(4.4).custom('integer') // throws error
is(8).custom('integer') // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 is(string).string()
 is(boolean).boolean()
 // continue with your code.
}
added 63 characters in body
Source Link
Calvintwr
  • 8.8k
  • 5
  • 33
  • 48

A clean approach

You can consider using a very small, dependency-free library like Not Issable. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
notis('number', 10number).number() // returns falsetrue
notis('number', []array).number() // throws error
// so you need to define your own:
letimport not{ define } from 'issable'
// or require syntax
const { define } = Object.createrequire(Not'issable')
not.defineTypedefine({
 primitiveprimitives: 'number',
 typenameOfTyping: 'integer',
 passtoPass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
not.notis('integer', 4.4) // gives error message
not.iscustom('integer', 4.4) // returnsthrows falseerror
not.is('integer', 8).customer('integer') // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 notis('string', string).string()
 notis('boolean', boolean).boolean()
 // continue with your code.
}

A clean approach

You can consider using a very small, dependency-free library like Not. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
not('number', 10) // returns false
not('number', []) // throws error
// so you need to define your own:
let not = Object.create(Not)
not.defineType({
 primitive: 'number',
 type: 'integer',
 pass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
not.not('integer', 4.4) // gives error message
not.is('integer', 4.4) // returns false
not.is('integer', 8) // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 not('string', string)
 not('boolean', boolean)
 // continue with your code.
}

A clean approach

You can consider using a very small, dependency-free library like Issable. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
is(number).number() // returns true
is(array).number() // throws error
// so you need to define your own:
import { define } from 'issable'
// or require syntax
const { define } = require('issable')
define({
 primitives: 'number',
 nameOfTyping: 'integer',
 toPass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
is(4.4).custom('integer') // throws error
is(8).customer('integer') // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 is(string).string()
 is(boolean).boolean()
 // continue with your code.
}
Commonmark migration
Source Link

A clean approach

#A clean approach YouYou can consider using a very small, dependency-free library like Not. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
not('number', 10) // returns false
not('number', []) // throws error
// so you need to define your own:
let not = Object.create(Not)
not.defineType({
 primitive: 'number',
 type: 'integer',
 pass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
not.not('integer', 4.4) // gives error message
not.is('integer', 4.4) // returns false
not.is('integer', 8) // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 not('string', string)
 not('boolean', boolean)
 // continue with your code.
}

#A clean approach You can consider using a very small, dependency-free library like Not. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
not('number', 10) // returns false
not('number', []) // throws error
// so you need to define your own:
let not = Object.create(Not)
not.defineType({
 primitive: 'number',
 type: 'integer',
 pass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
not.not('integer', 4.4) // gives error message
not.is('integer', 4.4) // returns false
not.is('integer', 8) // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 not('string', string)
 not('boolean', boolean)
 // continue with your code.
}

A clean approach

You can consider using a very small, dependency-free library like Not. Solves all problems:

// at the basic level it supports primitives
let number = 10
let array = []
not('number', 10) // returns false
not('number', []) // throws error
// so you need to define your own:
let not = Object.create(Not)
not.defineType({
 primitive: 'number',
 type: 'integer',
 pass: function(candidate) {
 // pre-ECMA6
 return candidate.toFixed(0) === candidate.toString()
 // ECMA6
 return Number.isInteger(candidate)
 }
})
not.not('integer', 4.4) // gives error message
not.is('integer', 4.4) // returns false
not.is('integer', 8) // returns true

If you make it a habit, your code will be much stronger. Typescript solves part of the problem but doesn't work at runtime, which is also important.

function test (string, boolean) {
 // any of these below will throw errors to protect you
 not('string', string)
 not('boolean', boolean)
 // continue with your code.
}
added 84 characters in body
Source Link
Calvintwr
  • 8.8k
  • 5
  • 33
  • 48
Loading
improved example
Source Link
Calvintwr
  • 8.8k
  • 5
  • 33
  • 48
Loading
Source Link
Calvintwr
  • 8.8k
  • 5
  • 33
  • 48
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /