DoneJS StealJS jQuery++ FuncUnit DocumentJS
5.33.3
6.0.0 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-define
        • static
          • types
        • types
          • PropDefinition
          • ValueOptions
        • behaviors
          • default
          • Default
          • get
          • identity
          • serialize
          • set
          • type
          • Type
          • value
      • 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
      • 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-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-live
      • can-view-model
      • can-view-nodelist
      • 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-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

Converts a value set on an instance into an appropriate value.

type(newValue, propertyName)

Given the set value, transform it into a value appropriate to be set. type is called before set.

import {DefineMap} from "can";

const Person = DefineMap.extend({
  age: {
    type: ( newValue, propertyName ) => {
      return +newValue;
    }
  }
});

const p = new Person();
p.age = "25";
console.log( "p.age is a", typeof p.age, "The value is", p.age ); //-> "p.age is a number. The value is 25"

Parameters

  1. newValue {*}:

    The value set on the property.

  2. propertyName {String}:

    The property name being set.

Returns

{*}:

The value that should be passed to set or (if there is no set property) the value to set on the map instance.

"typeName"

Sets the type to a named type in types. The default typeName is "observable".

import {DefineMap} from "can";

const Person = DefineMap.extend({
  age: {
    type: "number"
  }
});

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

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

Parameters

  1. typeName {String}:

    A named type in types.

{propDefinition}

A PropDefinition that defines an inline can-define/map/map type. For example:

import {DefineMap} from "can";

const Home = DefineMap.extend({
  address: {
    type: {
      street: "string",
      city: { type: "string", default: "Chicago" }
    }
  }
});

const myHouse = new Home({
  address: {
    street: "101 Example St."
  }
});

console.log( myHouse.serialize() ); //-> { address: {city: "Chicago", street: "101 Example St."} }

[Type|propDefinition]

Defines an inline can-define/list/list type that's an array of Type or inline propDefinition can-define/map/map instances. For example:

import {DefineMap} from "can";
import {Person} from "//unpkg.com/can-demo-models@5";

const List = DefineMap.extend({
  people: {
    type: [ Person ]
  },
  addresses: {
    type: [ {
      street: "string",
      city: "string"
    } ]
  }
});

const myList = new List({
  people: [ {first: "Justin", last: "Meyer"} ],
  addresses: [ {street: "11 Example Ave.", city: "Chicago"} ]
});

console.log( myList.serialize() ); //-> {
//   addresses: [ {city: "Chicago", street: "11 Example Ave."} ],
//   people: [ {first: "Justin", last: "Meyer"} ]
// }

Use

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

  • A type function that returns the type coerced value.
  • A named type in types.
  • An object that gets converted into an inline DefineMap.
  • An array that gets converted to an inline DefineList.

Basic Example

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

import {DefineMap} from "can";

const Map = DefineMap.extend( {
    count: { type: "number" },
    items: {
        type( newValue ) {
            if ( typeof newValue === "string" ) {
                return newValue.split( "," );
            } else if ( Array.isArray( newValue ) ) {
                return newValue;
            }
        }
    }
} );

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

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

Preventing Arrays and Objects from Automatic Conversion

When an array value is set, it is automatically converted into a DefineList. Likewise, objects are converted into DefineMap instances. This behavior can be prevented like the following:

In this example when a user tries to set the locations property, the resulting value will remain an array.

import {DefineMap, DefineList} from "can";

const MyMap = DefineMap.extend({
    locations: {type: "any"},
});
const map = new MyMap( {locations: [1, 2, 3]} );

// locations is an array, not a DefineList
console.log( map.locations instanceof DefineList ); //-> false
console.log( Array.isArray( map.locations ) ); //-> true

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

On this page

Get help

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