Created
August 6, 2019 03:07
-
-
Save Jexordexan/d858765f9e5aa566ca4bca274f444ab2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type PropType = String | Boolean | Number | Object | typeof Array | |
function prop<T = any>(type: PropType, options: any = {}): T { | |
return { type, ...options } as any | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Props { | |
user: api.User | |
alerts: api.Alert[] | |
} | |
export default createComponent({ | |
name: "UserAlerts", | |
props: { | |
user: Object, | |
alerts: Array, | |
} as any as Props, | |
setup(props) { | |
// Typescript infers typeof 'props' to be `Props` | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default createComponent({ | |
name: "UserAlerts", | |
props: { | |
user: Object as any as User, | |
alerts: Array as any as Alert[], | |
}, | |
setup(props) { | |
// Typescript infers typeof 'props' to be: | |
// { | |
// user: User, | |
// alerts: Alert[], | |
// } | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { prop } from 'ts-tricks-hidey-hole' | |
export default createComponent({ | |
name: "UserAlerts", | |
props: { | |
user: prop<User>(Object), | |
alerts: prop<Alert[]>(Array), | |
}, | |
setup(props) { | |
// Typescript infers typeof 'props' to be: | |
// { | |
// user: User, | |
// alerts: Alert[], | |
// } | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Props { | |
user: api.User | |
alerts?: api.Alert[] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { value, createComponent } from 'vue-function-api' | |
export default createComponent({ | |
props: /* ... */, | |
setup(props) { | |
const sort = state({ | |
field: 'created_at', | |
reverse: false, | |
}) | |
function changeSort(field: string, reverse: boolean) { | |
sort.field = field | |
sort.reverse = reverse | |
} | |
return { sort, changeSort } | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default createComponent({ | |
name: "UserAlerts", | |
props: { | |
user: Object, | |
alerts: Array, | |
}, | |
setup(props) { | |
// Typescript automatically infers typeof 'props' to be: | |
// { | |
// user: ObjectConstructor, | |
// alerts: ArrayConstructor, | |
// } | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { value, createComponent } from 'vue-function-api' | |
export default createComponent({ | |
props: /* ... */, | |
setup(props) { | |
const sortField = value('created_at') | |
const reverse = value(false) | |
function changeSort(field: string, reverse: boolean) { | |
sortField.value = field | |
reverse.value = reverse | |
} | |
return { sortField, reverse, changeSort } | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let name = 'Roger Rabbit' | |
watch(name, cb) // equal to watch('Roger Rabbit', cb) | |
name = 'Jessica Rabbit' | |
// watch is never called, name wasn't cahnged, it was completely reassigned |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const name = value('Roger Rabbit') | |
watch(name, cb) | |
name.value = 'Jessica Rabbit' | |
// watch IS called because the 'value' property was changed, not the name itself |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment