Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3bac33d
Update 2021 weakref types
leoelm Feb 9, 2023
5871e83
Add symbol to WeakRef and FinalizationRegistry
leoelm Feb 9, 2023
c0161d9
Update WeakSet keys
leoelm Mar 5, 2023
0596a4c
Add WeakMap symbol support
leoelm Mar 5, 2023
776c6a7
Resolve compilation and testing issues
leoelm Mar 14, 2023
4b02a2c
Add some test changes
leoelm Mar 16, 2023
0e809bf
Add testing for WeakRef and FinalizationRegistry
leoelm Mar 22, 2023
2241306
Undo lib changes
leoelm Mar 23, 2023
18ec290
Undo lib changes
leoelm Mar 23, 2023
0198657
Address PR comments
leoelm Mar 29, 2023
a103a0b
Update 2021 weakref types
leoelm Feb 9, 2023
e898ad0
Add symbol to WeakRef and FinalizationRegistry
leoelm Feb 9, 2023
8b90d0a
Update WeakSet keys
leoelm Mar 5, 2023
485aea5
Add WeakMap symbol support
leoelm Mar 5, 2023
6350617
Resolve compilation and testing issues
leoelm Mar 14, 2023
b1a8c37
Add some test changes
leoelm Mar 16, 2023
8413425
Add testing for WeakRef and FinalizationRegistry
leoelm Mar 22, 2023
f8470f1
Undo lib changes
leoelm Mar 23, 2023
ff83b5c
Undo lib changes
leoelm Mar 23, 2023
c3275ca
Address PR comments
leoelm Mar 29, 2023
0809ac2
Merge branch 'le/symbols-as-weak-map-keys' of github.com:leoelm/TypeS…
leoelm Mar 29, 2023
8c2fd56
Address PR comments
leoelm Apr 3, 2023
2eb0354
Add baselines
leoelm Apr 3, 2023
7563f5e
Address PR comments
leoelm Apr 5, 2023
71b47cb
Address PR comments
leoelm Apr 6, 2023
c966981
Merge branch 'le/symbol-as-weak-keys' into le/symbols-as-weak-map-keys
leoelm Apr 8, 2023
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
4 changes: 4 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ const libEntries: [string, string][] = [
["es2022.string", "lib.es2022.string.d.ts"],
["es2022.regexp", "lib.es2022.regexp.d.ts"],
["es2023.array", "lib.es2023.array.d.ts"],
["es2023.collection", "lib.es2023.collection.d.ts"],
["es2023.weakref", "lib.es2023.weakref.d.ts"],
["esnext.array", "lib.es2023.array.d.ts"],
["esnext.collection", "lib.es2023.collection.d.ts"],
["esnext.weakref", "lib.es2023.weakref.d.ts"],
["esnext.symbol", "lib.es2019.symbol.d.ts"],
["esnext.asynciterable", "lib.es2018.asynciterable.d.ts"],
["esnext.intl", "lib.esnext.intl.d.ts"],
Expand Down
14 changes: 10 additions & 4 deletions src/lib/es2015.collection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ interface ReadonlyMap<K, V> {
readonly size: number;
}

interface WeakMap<K extends object, V> {
interface WeakKeyTypes {
object: object;
}

type WeakKey = WeakKeyTypes[keyof WeakKeyTypes];

interface WeakMap<K extends WeakKey, V> {
/**
* Removes the specified element from the WeakMap.
* @returns true if the element was successfully removed, or false if it was not present.
Expand Down Expand Up @@ -107,7 +113,7 @@ interface ReadonlySet<T> {
readonly size: number;
}

interface WeakSet<T extends object> {
interface WeakSet<T extends WeakKey> {
/**
* Appends a new object to the end of the WeakSet.
*/
Expand All @@ -124,7 +130,7 @@ interface WeakSet<T extends object> {
}

interface WeakSetConstructor {
new <T extends object = object>(values?: readonly T[] | null): WeakSet<T>;
readonly prototype: WeakSet<object>;
new <T extends WeakKey = WeakKey>(values?: readonly T[] | null): WeakSet<T>;
readonly prototype: WeakSet<WeakKey>;
}
declare var WeakSet: WeakSetConstructor;
8 changes: 4 additions & 4 deletions src/lib/es2015.iterable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ interface MapConstructor {
new <K, V>(iterable?: Iterable<readonly [K, V]> | null): Map<K, V>;
}

interface WeakMap<K extends object, V> { }
interface WeakMap<K extends WeakKey, V> { }

interface WeakMapConstructor {
new <K extends object, V>(iterable: Iterable<readonly [K, V]>): WeakMap<K, V>;
new <K extends WeakKey, V>(iterable: Iterable<readonly [K, V]>): WeakMap<K, V>;
}

interface Set<T> {
Expand Down Expand Up @@ -189,10 +189,10 @@ interface SetConstructor {
new <T>(iterable?: Iterable<T> | null): Set<T>;
}

interface WeakSet<T extends object> { }
interface WeakSet<T extends WeakKey> { }

interface WeakSetConstructor {
new <T extends object = object>(iterable: Iterable<T>): WeakSet<T>;
new <T extends WeakKey = WeakKey>(iterable: Iterable<T>): WeakSet<T>;
}

interface Promise<T> { }
Expand Down
4 changes: 2 additions & 2 deletions src/lib/es2015.symbol.wellknown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ interface Map<K, V> {
readonly [Symbol.toStringTag]: string;
}

interface WeakMap<K extends object, V> {
interface WeakMap<K extends WeakKey, V> {
readonly [Symbol.toStringTag]: string;
}

interface Set<T> {
readonly [Symbol.toStringTag]: string;
}

interface WeakSet<T extends object> {
interface WeakSet<T extends WeakKey> {
readonly [Symbol.toStringTag]: string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/es2021.weakref.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface WeakRef<T extends object> {
interface WeakRef<T extends WeakKey> {
readonly [Symbol.toStringTag]: "WeakRef";

/**
Expand All @@ -15,7 +15,7 @@ interface WeakRefConstructor {
* Creates a WeakRef instance for the given target object.
* @param target The target object for the WeakRef instance.
*/
new<T extends object>(target: T): WeakRef<T>;
new<T extends WeakKey>(target: T): WeakRef<T>;
}

declare var WeakRef: WeakRefConstructor;
Expand Down
16 changes: 16 additions & 0 deletions src/lib/es2023.collection.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface WeakKeyTypes {
symbol: symbol;
}

interface WeakSet<T extends WeakKey> {}

interface WeakSetConstructor {
new <T extends WeakKey = WeakKey>(values?: readonly T[] | null): WeakSet<T>;
}

interface WeakMap<K extends WeakKey, V> {}

interface WeakMapConstructor {
new <K extends WeakKey = WeakKey, V = any>(entries?: readonly [K, V][] | null): WeakMap<K, V>;
readonly prototype: WeakMap<WeakKey, any>;
}
2 changes: 2 additions & 0 deletions src/lib/es2023.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/// <reference lib="es2022" />
/// <reference lib="es2023.array" />
/// <reference lib="es2023.collection" />
/// <reference lib="es2023.weakref" />
49 changes: 49 additions & 0 deletions src/lib/es2023.weakref.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
interface WeakKeyTypes {
symbol: symbol;
}

interface FinalizationRegistry<T> {
readonly [Symbol.toStringTag]: "FinalizationRegistry";

/**
* Registers an object with the registry.
* @param target The target object to register.
* @param heldValue The value to pass to the finalizer for this object. This cannot be the
* target object.
* @param unregisterToken The token to pass to the unregister method to unregister the target
* object. If provided (and not undefined), this must be an object. If not provided, the target
* cannot be unregistered.
*/
registerSymbol(target: symbol, heldValue: T, unregisterToken?: symbol): void;

/**
* Unregisters an object from the registry.
* @param unregisterToken The token that was used as the unregisterToken argument when calling
* register to register the target object.
*/
unregisterSymbol(unregisterToken: symbol): void;
}

interface FinalizationRegistryConstructor {
readonly prototype: FinalizationRegistry<any>;

/**
* Creates a finalization registry with an associated cleanup callback
* @param cleanupCallback The callback to call after an object in the registry has been reclaimed.
*/
new<T>(cleanupCallback: (heldValue: T) => void): FinalizationRegistry<T>;
}

declare var FinalizationRegistry: FinalizationRegistryConstructor;

interface WeakRef<T extends WeakKey> {
/**
* Returns the WeakRef instance's target object, or undefined if the target object has been
* reclaimed.
*/
deref(): T | undefined;
}

interface WeakRefConstructor {
new<T extends WeakKey>(target: T): WeakRef<T>;
}
2 changes: 2 additions & 0 deletions src/lib/libs.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"es2022.string",
"es2022.regexp",
"es2023.array",
"es2023.collection",
"es2023.weakref",
"esnext.intl",
"decorators",
"decorators.legacy",
Expand Down
37 changes: 37 additions & 0 deletions tests/baselines/reference/acceptSymbolAsWeakType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [acceptSymbolAsWeakType.ts]
const s: symbol = Symbol('s');

const ws = new WeakSet([s]);
ws.add(s);
ws.has(s);
ws.delete(s);

const wm = new WeakMap([[s, false]]);
wm.set(s, true);
wm.has(s);
wm.get(s);
wm.delete(s);

const wr = new WeakRef(s);
wr.deref();

const f = new FinalizationRegistry<symbol>(() => {});
f.registerSymbol(s, null);
f.unregisterSymbol(s);

//// [acceptSymbolAsWeakType.js]
const s = Symbol('s');
const ws = new WeakSet([s]);
ws.add(s);
ws.has(s);
ws.delete(s);
const wm = new WeakMap([[s, false]]);
wm.set(s, true);
wm.has(s);
wm.get(s);
wm.delete(s);
const wr = new WeakRef(s);
wr.deref();
const f = new FinalizationRegistry(() => { });
f.registerSymbol(s, null);
f.unregisterSymbol(s);
99 changes: 99 additions & 0 deletions tests/baselines/reference/dissallowSymbolAsWeakType.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
tests/cases/compiler/dissallowSymbolAsWeakType.ts(3,12): error TS2769: No overload matches this call.
Overload 1 of 2, '(iterable: Iterable<object>): WeakSet<object>', gave the following error.
Argument of type 'symbol[]' is not assignable to parameter of type 'Iterable<object>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<symbol, any>' is not assignable to type 'IteratorResult<object, any>'.
Type 'IteratorYieldResult<symbol>' is not assignable to type 'IteratorResult<object, any>'.
Type 'IteratorYieldResult<symbol>' is not assignable to type 'IteratorYieldResult<object>'.
Type 'symbol' is not assignable to type 'object'.
Overload 2 of 2, '(values?: readonly object[]): WeakSet<object>', gave the following error.
Type 'symbol' is not assignable to type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(4,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(5,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(6,11): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(8,12): error TS2769: No overload matches this call.
Overload 1 of 2, '(iterable: Iterable<readonly [object, boolean]>): WeakMap<object, boolean>', gave the following error.
Argument of type '[symbol, false][]' is not assignable to parameter of type 'Iterable<readonly [object, boolean]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<[symbol, false], any>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'.
Type 'IteratorYieldResult<[symbol, false]>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'.
Type 'IteratorYieldResult<[symbol, false]>' is not assignable to type 'IteratorYieldResult<readonly [object, boolean]>'.
Type '[symbol, false]' is not assignable to type 'readonly [object, boolean]'.
Type at position 0 in source is not compatible with type at position 0 in target.
Type 'symbol' is not assignable to type 'object'.
Overload 2 of 2, '(entries?: readonly [object, boolean][]): WeakMap<object, boolean>', gave the following error.
Type 'symbol' is not assignable to type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(9,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(10,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(11,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(12,11): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(14,24): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(18,12): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
tests/cases/compiler/dissallowSymbolAsWeakType.ts(19,14): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.


==== tests/cases/compiler/dissallowSymbolAsWeakType.ts (12 errors) ====
const s: symbol = Symbol('s');

const ws = new WeakSet([s]);
~~~~~~~~~~~~~~~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(iterable: Iterable<object>): WeakSet<object>', gave the following error.
!!! error TS2769: Argument of type 'symbol[]' is not assignable to parameter of type 'Iterable<object>'.
!!! error TS2769: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
!!! error TS2769: Type 'IteratorResult<symbol, any>' is not assignable to type 'IteratorResult<object, any>'.
!!! error TS2769: Type 'IteratorYieldResult<symbol>' is not assignable to type 'IteratorResult<object, any>'.
!!! error TS2769: Type 'IteratorYieldResult<symbol>' is not assignable to type 'IteratorYieldResult<object>'.
!!! error TS2769: Type 'symbol' is not assignable to type 'object'.
!!! error TS2769: Overload 2 of 2, '(values?: readonly object[]): WeakSet<object>', gave the following error.
!!! error TS2769: Type 'symbol' is not assignable to type 'object'.
ws.add(s);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
ws.has(s);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
ws.delete(s);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.

const wm = new WeakMap([[s, false]]);
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(iterable: Iterable<readonly [object, boolean]>): WeakMap<object, boolean>', gave the following error.
!!! error TS2769: Argument of type '[symbol, false][]' is not assignable to parameter of type 'Iterable<readonly [object, boolean]>'.
!!! error TS2769: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
!!! error TS2769: Type 'IteratorResult<[symbol, false], any>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'.
!!! error TS2769: Type 'IteratorYieldResult<[symbol, false]>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'.
!!! error TS2769: Type 'IteratorYieldResult<[symbol, false]>' is not assignable to type 'IteratorYieldResult<readonly [object, boolean]>'.
!!! error TS2769: Type '[symbol, false]' is not assignable to type 'readonly [object, boolean]'.
!!! error TS2769: Type at position 0 in source is not compatible with type at position 0 in target.
!!! error TS2769: Type 'symbol' is not assignable to type 'object'.
!!! error TS2769: Overload 2 of 2, '(entries?: readonly [object, boolean][]): WeakMap<object, boolean>', gave the following error.
!!! error TS2769: Type 'symbol' is not assignable to type 'object'.
wm.set(s, true);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
wm.has(s);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
wm.get(s);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
wm.delete(s);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.

const wr = new WeakRef(s);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
wr.deref();

const f = new FinalizationRegistry(() => {});
f.register(s, null);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
f.unregister(s);
~
!!! error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.

38 changes: 38 additions & 0 deletions tests/baselines/reference/dissallowSymbolAsWeakType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [dissallowSymbolAsWeakType.ts]
const s: symbol = Symbol('s');

const ws = new WeakSet([s]);
ws.add(s);
ws.has(s);
ws.delete(s);

const wm = new WeakMap([[s, false]]);
wm.set(s, true);
wm.has(s);
wm.get(s);
wm.delete(s);

const wr = new WeakRef(s);
wr.deref();

const f = new FinalizationRegistry(() => {});
f.register(s, null);
f.unregister(s);


//// [dissallowSymbolAsWeakType.js]
const s = Symbol('s');
const ws = new WeakSet([s]);
ws.add(s);
ws.has(s);
ws.delete(s);
const wm = new WeakMap([[s, false]]);
wm.set(s, true);
wm.has(s);
wm.get(s);
wm.delete(s);
const wr = new WeakRef(s);
wr.deref();
const f = new FinalizationRegistry(() => { });
f.register(s, null);
f.unregister(s);
22 changes: 22 additions & 0 deletions tests/cases/compiler/acceptSymbolAsWeakType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @lib: esnext
// @target: esnext

const s: symbol = Symbol('s');

const ws = new WeakSet([s]);
ws.add(s);
ws.has(s);
ws.delete(s);

const wm = new WeakMap([[s, false]]);
wm.set(s, true);
wm.has(s);
wm.get(s);
wm.delete(s);

const wr = new WeakRef(s);
wr.deref();

const f = new FinalizationRegistry<symbol>(() => {});
f.registerSymbol(s, null);
f.unregisterSymbol(s);
Loading