Skip to content

Commit

Permalink
strict type compare test
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Jun 9, 2019
1 parent c27fd76 commit e9f5dbb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
10 changes: 10 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["*"]
}
70 changes: 39 additions & 31 deletions test/type_test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
import ArSyncModel from './generated_typed_files/ArSyncModel'
import { useArSyncModel, useArSyncFetch } from './generated_typed_files/hooks'

type IsEqual<T, U> = [T, U] extends [U, T] ? true : false
function isOK<T extends true>(): T | undefined { return }
type IsStrictMode = string | null extends string ? false : true
isOK<IsStrictMode>()

const [hooksData1] = useArSyncModel({ field: 'currentUser', query: 'id' })
hooksData1!.id
isOK<IsEqual<typeof hooksData1, { id: number } | null>>()
const [hooksData2] = useArSyncModel({ field: 'currentUser', query: { '*': true, foo: true } })
hooksData2!.error.extraFields = 'foo'
isOK<IsEqual<typeof hooksData2, { error: { extraFields: 'foo' } } | null>>()
const [hooksData3] = useArSyncFetch({ field: 'currentUser', query: 'id' })
hooksData3!.id
isOK<IsEqual<typeof hooksData3, { id: number } | null>>()
const [hooksData4] = useArSyncFetch({ field: 'currentUser', query: { '*': true, foo: true } })
hooksData4!.error.extraFields = 'foo'
isOK<IsEqual<typeof hooksData4, { error: { extraFields: 'foo' } } | null>>()

const data1 = new ArSyncModel({ field: 'currentUser', query: 'id' }).data!
data1.id
isOK<IsEqual<typeof data1, { id: number }>>()
const data2 = new ArSyncModel({ field: 'currentUser', query: ['id', 'name'] }).data!
data2.id; data2.name
isOK<IsEqual<typeof data2, { id: number; name: string | null }>>()
const data3 = new ArSyncModel({ field: 'currentUser', query: '*' }).data!
data3.id; data3.name; data3.posts
isOK<IsEqual<typeof data3, { id: number; name: string | null; posts: {}[]; do_not_call_after_destroyed: any }>>()
const data4 = new ArSyncModel({ field: 'currentUser', query: { posts: 'id' } }).data!
data4.posts[0].id
isOK<IsEqual<typeof data4, { posts: { id: number }[] }>>()
const data5 = new ArSyncModel({ field: 'currentUser', query: { posts: '*' } }).data!
data5.posts[0].id; data5.posts[0].user; data5.posts[0].body
const data6 = new ArSyncModel({ field: 'currentUser', query: { posts: { '*': true, comments: 'user' } } }).data!
data6.posts[0].id; data6.posts[0].user; data6.posts[0].comments[0].user
isOK<IsEqual<typeof data5, {
posts: {
id: number; user: {}; title: string | null; body: string | null;
do_not_call_after_destroyed: any; comments: {}[]; my_comments: {}[]
}[]
}>>()
const data6 = new ArSyncModel({ field: 'currentUser', query: { posts: { '*': true, comments: 'user' } } }).data!.posts[0].comments[0]
isOK<IsEqual<typeof data6, { user: {} }>>()
const data7 = new ArSyncModel({ field: 'currentUser', query: { name: true, poosts: true } }).data!
data7.error.extraFields = 'poosts'
isOK<IsEqual<typeof data7, { error: { extraFields: 'poosts' } }>>()
const data8 = new ArSyncModel({ field: 'currentUser', query: { posts: { id: true, commmments: true, titllle: true } } }).data!
data8.error.extraFields = 'commmments'
data8.error.extraFields = 'titllle'
isOK<IsEqual<typeof data8, { error: { extraFields: 'commmments' | 'titllle' } }>>()
const data9 = new ArSyncModel({ field: 'currentUser', query: { '*': true, posts: { id: true, commmments: true } } }).data!
data9.error.extraFields = 'commmments'
const data10 = new ArSyncModel({ field: 'users', query: { '*': true, posts: { id: true, comments: '*' } } }).data!
data10[0].posts[0].comments[0].id
isOK<IsEqual<typeof data9, { error: { extraFields: 'commmments' } }>>()
const data10 = new ArSyncModel({ field: 'users', query: { '*': true, posts: { id: true, comments: '*' } } }).data![0].posts[0].comments[0].id
isOK<IsEqual<typeof data10, number>>()
const data11 = new ArSyncModel({ field: 'users', query: { '*': true, posts: { id: true, comments: '*', commmments: true } } }).data!
data11.error.extraFields = 'commmments'
const data12 = new ArSyncModel({ field: 'currentUser', query: { posts: { params: { limit: 4 }, query: 'title' } } }).data!
data12.posts[0].title
const data13 = new ArSyncModel({ field: 'currentUser', query: { posts: { params: { limit: 4 }, query: ['id', 'title'] } } }).data!
data13.posts[0].title
const data14 = new ArSyncModel({ field: 'currentUser', query: { posts: { params: { limit: 4 }, query: { id: true, title: true } } } }).data!
data14.posts[0].title
const data15 = new ArSyncModel({ field: 'currentUser', query: { posts: ['id', 'title'] } } as const).data!
data15.posts[0].title
isOK<IsEqual<typeof data11, { error: { extraFields: 'commmments' } }>>()
const data12 = new ArSyncModel({ field: 'currentUser', query: { posts: { params: { limit: 4 }, query: 'title' } } }).data!.posts[0]
isOK<IsEqual<typeof data12, { title: string | null }>>()
const data13 = new ArSyncModel({ field: 'currentUser', query: { posts: { params: { limit: 4 }, query: ['id', 'title'] } } }).data!.posts[0]
isOK<IsEqual<typeof data13, { id: number; title: string | null }>>()
const data14 = new ArSyncModel({ field: 'currentUser', query: { posts: { params: { limit: 4 }, query: { id: true, title: true } } } }).data!.posts[0]
isOK<IsEqual<typeof data14, { id: number; title: string | null }>>()
const data15 = new ArSyncModel({ field: 'currentUser', query: { posts: ['id', 'title'] } } as const).data!.posts[0]
isOK<IsEqual<typeof data15, { id: number; title: string | null }>>()

const data16 = new ArSyncModel({ field: 'currentUser', query: { id: { field: 'name' }, name: { field: 'id' }, id2: { field: 'id' }, name2: { field: 'name' } } }).data!
data16.name = data16.id2 = 0
data16.id = data16.name2 = 'name'
const data17 = new ArSyncModel({ field: 'currentUser', query: { posts: { '*': true, hoge: { field: 'comments', query: 'id' }, comments: { field: 'title' } } } }).data!
data17.posts[0].comments = 'title'
data17.posts[0].hoge[0].id = 0
isOK<IsEqual<typeof data16, { id: string | null; name: number; id2: number; name2: string | null }>>()
const data17 = new ArSyncModel({ field: 'currentUser', query: { posts: { '*': true, hoge: { field: 'comments', query: 'id' }, comments: { field: 'title' } } } }).data!.posts[0]
isOK<IsEqual<typeof data17.hoge, { id: number }[]>>()
isOK<IsEqual<typeof data17.comments, string | null>>()

0 comments on commit e9f5dbb

Please sign in to comment.