Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- run: npm ci
- run: npm run lint
- run: npm run build
- run: npm run test:offline-types:nonstrict
test:
runs-on: ubuntu-latest
strategy:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"lint": "ng lint",
"test:watch": "ng test",
"test:actions": "node --test .github/actions/classify-mobile-release/classify-mobile-release.spec.mjs && vitest run --config .github/actions/vitest.config.mjs",
"test:offline-types:nonstrict": "npm run prebuild:kit && tsc -p projects/kit/offline/tsconfig.nonstrict-null.json",
"e2e": "playwright test",
"e2e:ui": "playwright test --ui"
},
Expand Down
22 changes: 14 additions & 8 deletions projects/kit/offline/src/lib/offline-replica-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,20 @@ type NormalizeReplicaColumnValue<T> = T extends string
? string | Date
: T;

type IsNullableSelectValue<T> = null extends T ? true : undefined extends T ? true : false;

type OfflineReplicaColumnDefForValue<T> = IsNullableSelectValue<T> extends true
? OfflineReplicaColumnDef<
NormalizeReplicaColumnValue<StripNullish<T>>,
{ readonly [replicaNullableBrand]: 'nullable' }
>
: OfflineReplicaColumnDef<NormalizeReplicaColumnValue<T>, { readonly [replicaNullableBrand]: 'required' }>;
type StrictNullChecksEnabled = undefined extends null ? false : true;

type ReplicaColumnNullabilityForSelect<T> = StrictNullChecksEnabled extends true
? null extends T
? ReplicaNullableBrand & { readonly [replicaNullableBrand]: 'nullable' }
: undefined extends T
? ReplicaNullableBrand & { readonly [replicaNullableBrand]: 'nullable' }
: ReplicaNullableBrand & { readonly [replicaNullableBrand]: 'required' }
: ReplicaNullableBrand;

type OfflineReplicaColumnDefForValue<T> = OfflineReplicaColumnDef<
NormalizeReplicaColumnValue<StripNullish<T>>,
ReplicaColumnNullabilityForSelect<T>
>;

type OfflineReplicaFieldDefForKey<TSelect extends Record<string, unknown>, K extends keyof TSelect> =
| (StripNullish<TSelect[K]> extends number ? OfflineReplicaServerIdDef : never)
Expand Down
12 changes: 12 additions & 0 deletions projects/kit/offline/tsconfig.nonstrict-null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "es2022",
"moduleResolution": "bundler",
"noEmit": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": false,
"target": "es2022"
},
"files": ["type-tests/nonstrict-null.ts"]
}
74 changes: 74 additions & 0 deletions projects/kit/offline/type-tests/nonstrict-null.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
defineOfflineReplicaSchema,
defineReplicaEntity,
integer,
nullable,
serverId,
text,
} from '../../../../dist/kit/types/rdlabo-ionic-angular-kit-offline';

// Type alias intentionally exercises Record compatibility in a non-strict-null consumer.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
type NonStrictSelect = {
id: number;
title: string;
subtitle: string | null;
sortOrder: number;
};

const entity = defineReplicaEntity<NonStrictSelect>()({
table: 'nonstrict_items',
sourceKey: 'nonstrict_items',
scope: 'user',
fields: {
id: serverId(),
title: text(),
subtitle: nullable(text()),
sortOrder: integer(),
},
});

defineOfflineReplicaSchema({
version: 1,
entities: [entity],
migrations: [],
});

defineReplicaEntity<NonStrictSelect>()({
table: 'nonstrict_invalid_items',
sourceKey: 'nonstrict_invalid_items',
scope: 'user',
fields: {
id: serverId(),
// @ts-expect-error Primitive affinity checks remain active without strictNullChecks.
title: integer(),
subtitle: nullable(text()),
sortOrder: integer(),
},
});

defineReplicaEntity<NonStrictSelect>()({
table: 'nonstrict_missing_items',
sourceKey: 'nonstrict_missing_items',
scope: 'user',
// @ts-expect-error Exact-key validation still rejects a missing select property.
fields: {
id: serverId(),
title: text(),
subtitle: nullable(text()),
},
});

defineReplicaEntity<NonStrictSelect>()({
table: 'nonstrict_extra_items',
sourceKey: 'nonstrict_extra_items',
scope: 'user',
// @ts-expect-error Exact-key validation still rejects an unknown property.
fields: {
id: serverId(),
title: text(),
subtitle: nullable(text()),
sortOrder: integer(),
extra: text(),
},
});