From 50ac675513d8cb4926907596f1c1c812a4107b33 Mon Sep 17 00:00:00 2001 From: Mohammad Hasani Date: Mon, 27 May 2019 18:57:51 +0430 Subject: [PATCH 1/2] fix: nested DeepImmutable and Immutable types neutralization closes #65 --- src/types.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/types.ts b/src/types.ts index 22d50bd..d224a90 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,6 +17,10 @@ export type Immutable = T extends Primitive ? ReadonlyArray : T extends Map ? ReadonlyMap + : T extends ReadonlyArray + ? T + : T extends ReadonlyMap + ? T : Readonly export type DeepImmutable = T extends Primitive @@ -25,6 +29,10 @@ export type DeepImmutable = T extends Primitive ? DeepImmutableArray : T extends Map ? DeepImmutableMap + : T extends ReadonlyArray + ? DeepImmutableArray + : T extends ReadonlyMap + ? DeepImmutableMap : DeepImmutableObject export type Reducer = ( From c81803147d10b3ddc61cf17882f962d1e55e80dd Mon Sep 17 00:00:00 2001 From: Mohammad Hasani Date: Thu, 30 May 2019 23:51:01 +0430 Subject: [PATCH 2/2] test: add Immutable and DeepImmutable types test --- .../__snapshots__/types.dts.spec.ts.snap | 157 +++++++++++++++ src/__tests__/types.dts.spec.ts | 180 ++++++++++++++++++ 2 files changed, 337 insertions(+) create mode 100644 src/__tests__/__snapshots__/types.dts.spec.ts.snap create mode 100644 src/__tests__/types.dts.spec.ts diff --git a/src/__tests__/__snapshots__/types.dts.spec.ts.snap b/src/__tests__/__snapshots__/types.dts.spec.ts.snap new file mode 100644 index 0000000..27eb8c9 --- /dev/null +++ b/src/__tests__/__snapshots__/types.dts.spec.ts.snap @@ -0,0 +1,157 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"Function"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"boolean\\", boolean>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"function\\", Function>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"null\\", null>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"number\\", number>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"string\\", string>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"undefined\\", undefined>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"user\\", User>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"users\\", User[]>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"boolean\\", boolean>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"function\\", Function>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"null\\", null>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"number\\", number>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"string\\", string>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"undefined\\", undefined>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"user\\", User>"`; + +exports[`DeepImmutable displayType>>() (type) should match snapshot: DeepImmutable displayType>>() 1`] = `"DeepImmutableMap<\\"users\\", User[]>"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableObject"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"boolean"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"null"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"number"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"string"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableArray"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"undefined"`; + +exports[`DeepImmutable displayType>() (type) should match snapshot: DeepImmutable displayType>() 1`] = `"DeepImmutableArray"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"Function"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"boolean\\", boolean>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"function\\", Function>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"null\\", null>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"number\\", number>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"string\\", string>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"undefined\\", undefined>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"user\\", User>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"users\\", User[]>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"boolean\\", boolean>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"function\\", Function>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"null\\", null>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"number\\", number>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"string\\", string>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"undefined\\", undefined>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"user\\", User>"`; + +exports[`Immutable displayType>>() (type) should match snapshot: Immutable displayType>>() 1`] = `"ReadonlyMap<\\"users\\", User[]>"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"Readonly"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"boolean"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"null"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"number"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"string"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"ReadonlyArray"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"undefined"`; + +exports[`Immutable displayType>() (type) should match snapshot: Immutable displayType>() 1`] = `"ReadonlyArray"`; diff --git a/src/__tests__/types.dts.spec.ts b/src/__tests__/types.dts.spec.ts new file mode 100644 index 0000000..5a79fe5 --- /dev/null +++ b/src/__tests__/types.dts.spec.ts @@ -0,0 +1,180 @@ +import { DeepImmutable, Immutable } from '../types' + +function displayType(): T { + return undefined +} + +type User = { name: string; age: number; children: Users; parents: Users } +type Users = User[] + +// @dts-jest:group Immutable + +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() + +// @dts-jest:pass:snap +displayType>() + +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() + +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() + +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() + +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() + +// @dts-jest:group DeepImmutable + +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() + +// @dts-jest:pass:snap +displayType>() + +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() +// @dts-jest:pass:snap +displayType>() + +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() + +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() + +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>() +// @dts-jest:pass:snap +displayType>>()