Skip to main content

Kea-TypeGen 1.0: Auto Import

ยท One min read
Marius Andra

Finally, files generated with kea-typegen will automatically import any types they can, and add the rest as type arguments for kea<logicType<LocalType, LocalUser>>

You just need to add types to your actions and reducers.

import { Blog } from './blog'
import { logicType } from './logicType'

export const LocalType = 'YES' | 'NO'

const logic = kea<logicType<LocalType>>({ // ๐Ÿ‘ˆ๐Ÿฆœ managed automatically by typegen
actions: {
openBlog: (id: number, blog?: Blog) => ({ id, blog }), // ๐Ÿ‘ˆ add types here
closeBlog: (answer: LocalType) => ({ answer }),
},
reducers: {
blogId: [
null as number | null, // ๐Ÿ‘ˆ null now, but sometimes a number ๐Ÿ™€
{
openBlog: (_, { id }) => id,
closeBlog: () => null,
// use `actionTypes` instead of `actions`
[funLogic.actionTypes.randomBlogPage]: () => 4, // chosen by a fair dice roll
},
],
},
listeners: () => ({
closeBlog: ({ answer }) => { // no types needed here
console.log(answer)
}
})
})

Read the updated TypeScript guide to learn more.

Questions & Answers