前提
JotaiというTypeScriptのライブラリがあり、中身を読み始めてみたのですが、
調べても仕組みが分からない点があり、ご存じの方がおられましたら教えていただきたいです。
聞きたいこと
atomの関数が複数あるのですが、エラーにならない原理がわかりません。
関数が重複しているエラーが出ると思うのですが、何故問題ないのでしょうか?
対象ソース
src/core/atom.ts
https://github.com/pmndrs/jotai
該当のソースコード
typescript
1// writable derived atom 2export function atom<Value, Update, Result extends void | Promise<void> = void>( 3 read: Read<Value>, 4 write: Write<Update, Result> 5): WritableAtom<Value, Update, Result> 6 7// read-only derived atom 8export function atom<Value>(read: Read<Value>): Atom<Value> 9 10// invalid function in the first argument 11export function atom(invalidFunction: (...args: any) => any, write?: any): never 12 13// write-only derived atom 14export function atom<Value, Update, Result extends void | Promise<void> = void>( 15 initialValue: Value, 16 write: Write<Update, Result> 17): WritableAtom<Value, Update, Result> & WithInitialValue<Value> 18 19// primitive atom 20export function atom<Value>( 21 initialValue: Value 22): PrimitiveAtom<Value> & WithInitialValue<Value> 23 24 25export function atom<Value, Update, Result extends void | Promise<void>>( 26 read: Value | Read<Value>, 27 write?: Write<Update, Result> 28) { 29 const key = `atom${++keyCount}` 30 const config = { 31 toString: () => key, 32 } as WritableAtom<Value, Update, Result> & { init?: Value } 33 if (typeof read === 'function') { 34 config.read = read as Read<Value> 35 } else { 36 config.init = read 37 config.read = (get) => get(config) 38 config.write = (get, set, update) => 39 set(config, typeof update === 'function' ? update(get(config)) : update) 40 } 41 if (write) { 42 config.write = write 43 } 44 return config 45}