can-define icon indicating copy to clipboard operation
can-define copied to clipboard

Proposal: Expanded Types and Required Properties

Open phillipskevin opened this issue 7 years ago • 1 comments

tldr

To make can-define more useful and add some long-requested functionality, we will:

  • add support for things like prop: String
  • expand the available Types
  • add strict Types
  • add support for required properties

This was discussed on a recent live stream (28:12)

Details

Allow shorthand with built-in Constructors

Instead of type: "string", type: "number", etc we should add support for using built-in constructors with Type like:

var VM = can.DefineMap.extend({
  prop: String
});

More Types

Also, we should add additional types. This is not a definitive list, just some things that have been requested and others that exist in other languages:

  • float
  • integer
  • character
  • Set
  • enum / union
  • tuple

Strict Types

We could also create strict types that would throw errors if set to a value of a different type. This might look something like:

import { StrictString } from 'can-define-strict-types';
import DefineMap from 'can-define/map/map';

const VM = DefineMap.extend({
    prop: StrictString
});

This may also be called MustString or something like that... we can bikeshed on naming later.

Required Values

Tangentially related to Types, people have also asked for support for required properties. While expanding the Type behavior, we would also add support for this.

phillipskevin avatar Nov 03 '17 01:11 phillipskevin

@christopherjbaker pointed out that we should take a look at https://www.npmjs.com/package/prop-types for inspiration.

phillipskevin avatar Nov 03 '17 17:11 phillipskevin