11import type { Writable } from '../../../utility-types.js'
2- import {
3- functionParameter ,
4- functionReturn ,
5- typeParameterAssignableToConstraint ,
6- type KeyPath ,
7- } from '../key-path.js'
2+ import type { Atom } from '../../parsing.js'
83import { types } from '../type-system.js'
94import { simplifyUnionType } from './subtyping.js'
105import {
@@ -19,14 +14,27 @@ import {
1914 type UnionType ,
2015} from './type-formats.js'
2116
17+ const functionParameter = Symbol ( 'functionParameter' )
18+ const functionReturn = Symbol ( 'functionReturn' )
19+ const typeParameterAssignableToConstraint = Symbol (
20+ 'typeParameterAssignableToConstraint' ,
21+ )
22+
23+ export type TypeKeyPath = readonly (
24+ | Atom
25+ | typeof functionParameter
26+ | typeof functionReturn
27+ | typeof typeParameterAssignableToConstraint
28+ ) [ ]
29+
2230type StringifiedKeyPath = string // this could be branded if that seems useful
2331type UnionOfTypeParameters = Omit < UnionType , 'members' > & {
2432 readonly members : ReadonlySet < TypeParameter >
2533}
2634export type TypeParametersByKeyPath = Map <
2735 StringifiedKeyPath ,
2836 {
29- readonly keyPath : KeyPath
37+ readonly keyPath : TypeKeyPath
3038 readonly typeParameters : UnionOfTypeParameters
3139 }
3240>
@@ -36,7 +44,7 @@ export const containedTypeParameters = (type: Type): TypeParametersByKeyPath =>
3644
3745const containedTypeParametersImplementation = (
3846 type : Type ,
39- root : KeyPath ,
47+ root : TypeKeyPath ,
4048) : TypeParametersByKeyPath => {
4149 // Avoid infinite recursion when we hit the top type.
4250 if ( type === types . something ) {
@@ -96,14 +104,14 @@ const containedTypeParametersImplementation = (
96104export const findKeyPathsToTypeParameter = (
97105 type : Type ,
98106 typeParameterToFind : TypeParameter ,
99- ) : Set < KeyPath > =>
107+ ) : Set < TypeKeyPath > =>
100108 findKeyPathsToTypeParameterImplementation ( type , typeParameterToFind , [ ] )
101109
102110const findKeyPathsToTypeParameterImplementation = (
103111 type : Type ,
104112 typeParameterToFind : TypeParameter ,
105- root : KeyPath ,
106- ) : Set < KeyPath > => {
113+ root : TypeKeyPath ,
114+ ) : Set < TypeKeyPath > => {
107115 // Avoid infinite recursion when we hit the top type.
108116 if ( type === types . something ) {
109117 return new Set ( )
@@ -148,7 +156,7 @@ const findKeyPathsToTypeParameterImplementation = (
148156 union : ( { members } ) =>
149157 [ ...members ]
150158 . map (
151- ( member ) : Set < KeyPath > =>
159+ ( member ) : Set < TypeKeyPath > =>
152160 typeof member === 'string'
153161 ? new Set ( )
154162 : findKeyPathsToTypeParameterImplementation (
@@ -253,7 +261,7 @@ export const supplyTypeArgument = (
253261 */
254262export const updateTypeAtKeyPathIfValid = (
255263 type : Type ,
256- keyPath : KeyPath ,
264+ keyPath : TypeKeyPath ,
257265 // TODO: `operation` should be able to update `Atom`s
258266 operation : ( typeAtKeyPath : Exclude < Type , UnionType > ) => Type ,
259267) : Type => {
@@ -400,7 +408,7 @@ const mergeTypeParametersByKeyPath = (
400408
401409// The string format is not meant for human consumption. The only guarantee is that every distinct
402410// key path produces a unique string.
403- const stringifyKeyPath = ( keyPath : KeyPath ) : string =>
411+ const stringifyKeyPath = ( keyPath : TypeKeyPath ) : string =>
404412 keyPath . reduce ( ( stringifiedKeyPath : string , key ) => {
405413 const stringifiedKey =
406414 typeof key === 'symbol' ? key . description : JSON . stringify ( key )
0 commit comments