DoneJS StealJS jQuery++ FuncUnit DocumentJS
6.0.1
5.33.2 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-deep-observable
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
        • behaviors
          • async
          • default
          • enumerable
          • get
          • get default()
          • identity
          • required
          • serialize
          • set
          • type
          • value
        • static
          • propertyDefaults
          • props
          • seal
        • prototype
          • get property()
          • set property()
        • types
          • DefinitionObject
          • Property
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-observable-bindings
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-model
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-define-realtime-rest-model
      • can-define-rest-model
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

type

  • Edit on GitHub

Specify a type for the property.

PrimitiveFunction

Sets the type to a primitive constructor function. Valid primitive constructors includes String, Number, and Boolean.

If the value provided for this property is of another type, or is null or undefined, it will throw.

import { ObservableObject } from "can/everything";

class Person extends ObservableObject {
  static props = {
    age: {
      type: Number
    }
  };
}

const p = new Person({ age: 5 });

console.log( p.age ) //-> 5

const p2 = new Person({ age: "5" }); //throws

Parameters

  1. PrimitiveFunction {function}:

    A primitive constructor function.

ConstructorFunction

Sets the type to a constructor function.

If the value provided for this property is of another type, or is null or undefined, it will throw.

import { ObservableObject } from "can/everything";

class Person extends ObservableObject {
  static props = {
    birthday: {
      type: Date
    }
  };
}

const p = new Person({ birthday: new Date(1970, 2, 3) });

console.log( p.age ) //-> 5

const p2 = new Person({ birthday: "2/3/1970" }); //throws

Parameters

  1. ConstructorFunction {function}:

    Any constructor function.

TypeObject

Defines a type that conforms to the TypeObject API: an object with a can.new and can.isMember symbol.

For example here is an inline TypeObject that behaves like type.convert(Date):

import { ObservableObject } from "can/everything";

class Person extends ObservableObject {
  static props = {
    birthday: {
      type: {
        [Symbol.for("can.new")](value) {
          return new Date(value);
        },
        [Symbol.for("can.isMember")](value) {
          return (value instanceof Date);
        }
      }
    }
  };
}

Use

The type property specifies the type of the attribute. The type can be specified as either:

  • A primitive constructor function.
  • A built-in constructor function like Date.
  • A constructor function such as another ObservableObject.
  • A TypeObject.

Basic Example

The following example converts the count property to a number and the items property to an array.

import { ObservableObject, type } from "can";

const ArrayType = {
  [Symbol.for("can.new")]( newValue ) {
    if ( typeof newValue === "string" ) {
      return newValue.split( "," );
    } else if ( Array.isArray( newValue ) ) {
      return newValue;
    }
  },
  [Symbol.for("can.isMember")]( value ) {
    return Array.isArray(value);
  }
};

class Map extends ObservableObject {
  static props = {
    count: {
      type: Number
    },
    items: {
      type: ArrayType
    }
  };
}

const map = new Map({ count: "4", items: "1,2,3" });

console.log(map.count, map.items); //-> 4 ["1", "2", "3"]

Shorthand

The Property docs specify shorthands for setting the type. The shorthand allows you to define the type without using a DefinitionObject like so:

import { ObservableObject } from "can/everything";

class Person extends ObservableObject {
  age: Number
}

Use with can-type

can-type provides convenient ways to use types with a variety of behaviors like type checking and converting. You can use these with the Property shorthand like so:

import { ObservableObject, type } from "can/everything";

class Person extends ObservableObject {
  age: type.convert(Number)
}

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 6.0.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news