Skip to content

Commit ebf2c23

Browse files
committed
Change typing of application specific ArSyncModel export
1 parent 814ef73 commit ebf2c23

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

lib/ar_sync/type_script.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ def self.generate_model_script
5353
import { TypeRequest } from './types'
5454
import DataTypeFromRequest from './DataTypeFromRequest'
5555
import { default as ArSyncModelBase } from 'ar_sync/core/ArSyncModel'
56-
declare class ArSyncModel<R extends TypeRequest> extends ArSyncModelBase<DataTypeFromRequest<R>> {
57-
constructor(r: R, option?: { immutable: boolean })
56+
import ConnectionAdapter from 'ar_sync/core/ConnectionAdapter'
57+
58+
type ArSyncModel<R extends TypeRequest> = ArSyncModelBase<DataTypeFromRequest<R>>
59+
const ArSyncModel = ArSyncModelBase as unknown as {
60+
new <R extends TypeRequest>(r: R, option?: { immutable: boolean }): ArSyncModel<R>
61+
setConnectionAdapter(adapter: ConnectionAdapter): void
5862
}
59-
export default ArSyncModelBase as typeof ArSyncModel
63+
export default ArSyncModel
6064
CODE
6165
end
6266

test/ts_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@ def test_typed_files
2626
output = output.lines.grep(/type_test/)
2727
puts output
2828
assert output.empty?
29+
30+
output = `./node_modules/typescript/bin/tsc --lib es2017,dom --noEmit test/generated_typed_files/*.ts`
31+
puts output
32+
assert output.empty?
2933
end
3034
end

test/type_test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as ActionCable from 'actioncable'
55
ArSyncModel.setConnectionAdapter(new ActionCableAdapter(ActionCable))
66

77
type IsEqual<T, U> = [T, U] extends [U, T] ? true : false
8+
type IsNotEqual<T, U> = IsEqual<T, U> extends true ? false : true
89
function isOK<T extends true>(): T | undefined { return }
910
type IsStrictMode = string | null extends string ? false : true
1011
type TypeIncludes<T extends {}, U extends {}> = IsEqual<Pick<T, keyof T & keyof U>, U>
@@ -13,6 +14,7 @@ isOK<IsStrictMode>()
1314

1415
const [hooksData1] = useArSyncModel({ api: 'currentUser', query: 'id' })
1516
isOK<IsEqual<typeof hooksData1, { id: number } | null>>()
17+
isOK<IsNotEqual<typeof hooksData1, { id: string } | null>>()
1618
const [hooksData2] = useArSyncModel({ api: 'currentUser', query: { '*': true, foo: true } })
1719
isOK<HasExtraField<typeof hooksData2, 'foo'>>()
1820
const [hooksData3] = useArSyncFetch({ api: 'currentUser', query: 'id' })
@@ -22,6 +24,7 @@ isOK<HasExtraField<typeof hooksData4, 'foo'>>()
2224

2325
const data1 = new ArSyncModel({ api: 'currentUser', query: 'id' }).data!
2426
isOK<IsEqual<typeof data1, { id: number }>>()
27+
isOK<IsNotEqual<typeof data1, { id: string }>>()
2528
const data2 = new ArSyncModel({ api: 'currentUser', query: ['id', 'name'] }).data!
2629
isOK<IsEqual<typeof data2, { id: number; name: string | null }>>()
2730
const data3 = new ArSyncModel({ api: 'currentUser', query: '*' }).data!

0 commit comments

Comments
 (0)