Skip to content

Instantly share code, notes, and snippets.

@Pictor13
Created July 28, 2023 03:24
Show Gist options
  • Save Pictor13/b9d2231bf4d81c9a138ea723f6b11043 to your computer and use it in GitHub Desktop.
Save Pictor13/b9d2231bf4d81c9a138ea723f6b11043 to your computer and use it in GitHub Desktop.
Generic concerns separation
// benefits? disadvantages?
// state type-interface
type State = {
name: string,
age: number,
gender: string,
hobby: Array<string>
}
// Zod validator & Type<infer> on State
// state mutations
const changeGender = newAssignedGender: string => this.gender = newAssignedGender
const changeName = newAssignedName: string => this.name = newAssignedName
const haveNewHobby = newHobby: string => this.hobby.push(newHobby)
const abandonHobby = hobbyToRemove: string => this.hobby = this.hobby.filter(hobby => hobby!== hobbyToRemove)
// state getters
const getName = state: State => state.name
const getAge = state: State => state.age
const getGender = state: State => state.gender
const getHobby = state: State => state.hobby
const isOver18 = state: State => state.age >= 18
const getGenderPrefix = state: State => state.gender === 'M'? 'Mr.' : state.gender === 'F'? 'Mrs.' : 'Messrs'
const getNominative = state: State => `${getGenderPrefix} ${state.name}`
// behaviour interfaces
interface function name(params): object
// behaviors
function name(params: State) {
// do something with params
}
const state: State = {
name: 'Alfonso Purcicuddu',
age: 54,
gender: 'Q',
hobby: []
}
name(state)
changeGender('M')
changeName('Bisboccio Purcicuddu')
console.log(`Hello getNominative(state)`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment