11import { expectType , expectError , expectAssignable } from 'tsd' ;
2- import type { ReadonlyDeep , Writable , WritableDeep } from '../index' ;
2+ import type { JsonValue , Opaque , ReadonlyDeep , WritableDeep } from '../index' ;
33import type { WritableObjectDeep } from '../source/writable-deep' ;
4+ import { type tag } from '../source/opaque' ;
45
56type Overloaded = {
67 ( foo : number ) : string ;
@@ -16,6 +17,17 @@ type NamespaceWithOverload = Overloaded & {
1617 readonly baz : readonly boolean [ ] ;
1718} ;
1819
20+ type OpaqueObjectData = { readonly a : number [ ] } | { readonly b : string } ;
21+ type OpaqueObject = Opaque < OpaqueObjectData , { readonly token : unknown } > ;
22+
23+ type ReadonlyJsonValue =
24+ | { readonly [ k : string ] : ReadonlyJsonValue }
25+ | readonly ReadonlyJsonValue [ ]
26+ | number
27+ | string
28+ | boolean
29+ | null ;
30+
1931const data = {
2032 object : {
2133 foo : 'bar' ,
@@ -35,14 +47,21 @@ const data = {
3547 map : new Map < string , string > ( ) ,
3648 set : new Set < string > ( ) ,
3749 array : [ 'foo' ] ,
50+ emptyTuple : [ ] as [ ] ,
3851 tuple : [ 'foo' ] as [ 'foo' ] ,
52+ multiItemTuple : [ { a : '' } , { b : 1 } ] as [ { a : string } , { b : number } ] ,
53+ spreadTuple : [ 'foo' ] as [ ...string [ ] ] ,
54+ trailingSpreadTuple : [ 'foo' , 1 ] as [ string , ...number [ ] ] ,
55+ leadingSpreadTuple : [ 'foo' , 1 ] as [ ...string [ ] , number ] ,
3956 readonlyMap : new Map < string , string > ( ) as ReadonlyMap < string , string > ,
4057 readonlySet : new Set < string > ( ) as ReadonlySet < string > ,
4158 readonlyArray : [ 'foo' ] as readonly string [ ] ,
4259 readonlyTuple : [ 'foo' ] as const ,
60+ json : [ { x : true } ] as JsonValue ,
61+ opaqueObj : { a : [ 3 ] } as OpaqueObject , // eslint-disable-line @typescript-eslint/consistent-type-assertions
4362} ;
4463
45- const readonlyData : Readonly < typeof data > = data ;
64+ const readonlyData : ReadonlyDeep < typeof data > = data ;
4665
4766let writableData : WritableDeep < typeof readonlyData > ;
4867expectError ( writableData = readonlyData ) ;
@@ -63,14 +82,21 @@ expectType<null>(writableData.null);
6382expectType < undefined > ( writableData . undefined ) ;
6483expectType < Date > ( writableData . date ) ;
6584expectType < RegExp > ( writableData . regExp ) ;
66- expectType < Writable < Map < string , string > > > ( writableData . map ) ;
67- expectType < Writable < Set < string > > > ( writableData . set ) ;
85+ expectType < Map < string , string > > ( writableData . map ) ;
86+ expectType < Set < string > > ( writableData . set ) ;
6887expectType < string [ ] > ( writableData . array ) ;
88+ expectType < [ ] > ( writableData . emptyTuple ) ;
6989expectType < [ 'foo' ] > ( writableData . tuple ) ;
70- expectType < Writable < Map < string , string > > > ( writableData . readonlyMap ) ;
71- expectType < Writable < Set < string > > > ( writableData . readonlySet ) ;
90+ expectType < [ { a : string } , { b : number } ] > ( writableData . multiItemTuple ) ;
91+ expectType < [ ...string [ ] ] > ( writableData . spreadTuple ) ;
92+ expectType < [ string , ...number [ ] ] > ( writableData . trailingSpreadTuple ) ;
93+ expectType < [ ...string [ ] , number ] > ( writableData . leadingSpreadTuple ) ;
94+ expectType < Map < string , string > > ( writableData . readonlyMap ) ;
95+ expectType < Set < string > > ( writableData . readonlySet ) ;
7296expectType < string [ ] > ( writableData . readonlyArray ) ;
7397expectType < [ 'foo' ] > ( writableData . readonlyTuple ) ;
98+ expectAssignable < ReadonlyJsonValue > ( writableData . json ) ;
99+ expectAssignable < Opaque < WritableDeep < OpaqueObjectData > , WritableDeep < OpaqueObject [ typeof tag ] > > > ( writableData . opaqueObj ) ;
74100
75101expectType < ( ( foo : number ) => string ) & WritableObjectDeep < Namespace > > ( writableData . namespace ) ;
76102expectType < string > ( writableData . namespace ( 1 ) ) ;
0 commit comments