Comments (2)
@thom-nic with Record
types you could define Person
type like in the example below. Defined records will have all the getters for fields defined:
const Person = Record({first: String, last:String})
const bob = Record({first: 'Bob', last: 'Fish'})
bob.first // => 'Bob'
bob.last // => 'Fish
As of computed field like fullName
I personally like to use plain functions:
fullName = ({first, last}) => `${first} ${last}`
I used to put such functions as a static methods of the Record constructor Person.fullName = fullName
just for grouping, but I ditched that practice and tend to group functions into modules instead.
You could also define them as methods on the prototype if you like:
const asMethod = f => function(...args) => {f(this, ....args)}
Person.prototype.fullName = asMethod(fullName)
That is sometimes useful if you want a polymorphism behavior, although for that I still tend to use method library or if I don't want pull in extra library then this pattern:
const slice = (value, from, to) => value.constructor[$slice](value, from, to)
$slice = slice.symbol = Symbol.for('mylib/slice')
MyType[slice.symbol] = (myType, from, to) => /*... */
MyOtherType[slice.symbol] = (myOtherType, from, to) => /*... */
I prefer functions because:
- I like to keep data separate from operations that can be performed on it. That way all operations are equal no first class second class.
- Functions are more convenient for functional compositions and with high order functions like .map
.filter etc...
Ultimately I think it's really just matter of personal preference and does not really matter.
from typed-immutable.
Oh and I don't personally like property getters for computed properties as I think they live an impression of being regular property while under the hood they do run code that may cause side-effects throw exception etc.. I much rather prefer to be able to distinguish computation from property access while reading a code without having to digg into implementations.
P.S. Record do use getters under the hood though, primarily so that they would throw exceptions at you if you try to mutate them and to make assignment possible on .asMutable()
API.
from typed-immutable.
Related Issues (20)
- flatMap doesn't work HOT 3
- Handle recursive types? HOT 2
- Handle recursive types? HOT 3
- Support for Enumerable fields
- Immutable List insert() HOT 1
- New maintainer is needed HOT 29
- Process discussion HOT 12
- Typed data must carry type information HOT 1
- Minor release HOT 6
- Add functions?
- setSize may only downsize HOT 4
- Consider adding type coercion HOT 3
- List.map does not work with mapper that returns null or undefined HOT 1
- Add pattern matching HOT 1
- React PropType validations HOT 1
- Better error messages HOT 3
- Symbol polyfill overwrites native Symbol type in typed.js
- Type checking task HOT 2
- Support for Map HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typed-immutable.