You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeBaseItem<T=any>={id: T}interfaceSnapshot<TextendsBaseItem<I>=BaseItem,I=any>{id: string,time: number,collectionName: string,items: T[],}constselector: FlatQuery<Snapshot<BaseItem>>={collectionName: 'asdf'}// ^^// No error β functiontest<ItemTypeextendsBaseItem<IdType>,IdType=any>(){typeItemKeys=DotNotationKeys<Snapshot<ItemType,IdType>>// ^^// "id" | "time" | "collectionName" | "items" | `items.${string}`typeCollectionNameType=Flatten<Get<Snapshot<ItemType,IdType>,'collectionName'>>// ^^// "string"constgenericSelector: FlatQuery<Snapshot<ItemType,IdType>>={collectionName: 'asdf'}// ^^// Error β: Type '{ collectionName: string; }' is not assignable to type 'FlatQuery<Snapshot<ItemType, IdType>>'.}// Utility type to check if a type is an array or object.typeIsObject<T>=Textendsobject ? (TextendsArray<any> ? false : true) : false// Recursive type to generate dot-notation keystypeDotNotationKeys<T>={[KinkeyofT&(string|number)]:
T[K]extendsArray<infer U>// If it's an array, include both the index and the $ wildcard
? `${K}` | `${K}.$` | `${K}.${DotNotationKeys<U>}`
// If it's an object, recurse into it
: IsObject<T[K]>extendstrue
? `${K}` | `${K}.${DotNotationKeys<T[K]>}`
: `${K}` // Base case: Just return the key}[keyofT&(string|number)]typeSplit<Sextendsstring,Delimiterextendsstring>=Sextends `${infer Head}${Delimiter}${infer Tail}`
? [Head, ...Split<Tail,Delimiter>]
: [S]typeGetTypeByParts<T,Partsextendsreadonlystring[]>=Partsextends[infer Head, ...infer Tail]
? HeadextendskeyofT
? GetTypeByParts<T[Head],Extract<Tail,string[]>>
: Headextends'$'
? TextendsArray<infer U>
? GetTypeByParts<U,Extract<Tail,string[]>>
: never
: never
: TtypeGet<T,Pathextendsstring>=GetTypeByParts<T,Split<Path,'.'>>typeFlatten<T>=Textendsany[] ? T[0] : TtypeFlatQuery<T>={[PinDotNotationKeys<T>]?: Flatten<Get<T,P>>}
π Actual behavior
Typescript isn't able to assign { collectionName: 'asdf' } when using generic types:
functiontest<ItemTypeextendsBaseItem<IdType>,IdType=any>(){constselector: FlatQuery<Snapshot<ItemType,IdType>>={collectionName: 'asdf'}// ^^// Type '{ collectionName: string; }' is not assignable to type 'FlatQuery<Snapshot<ItemType, IdType>>'.}
We really need a less (much less) complex repro in order to have any chance of investigating this before other issues which have much more straightforward repros. Please try to get this down to a minimal sample if you'd like us to look at it. Thanks!
I'm not the person who opened this issue, but ended up looking at it and I think the core problem essentially boils down to this:
typeExample<Textends'b'|'c'>=Partial<{a: unknown}&Record<T,unknown>><Textends'b'|'c'>()=>{constproblem: Example<T>={a: null}// ^^^^^^^// Type '{ a: null; }' is not assignable to type 'Partial<{ a: unknown; } & Record<T, unknown>>'.}
Any instantiated Example<ConcreteType> will allow an object containing only the key a to be assigned to it, so maybe Example<T> (where T is a type parameter) should also allow it?
π Search Terms
error recursive generic types
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.6.3#code/C4TwDgpgBAQghgZwgSWBAtgHgCpQLxRwB2IAfPlAN5QCWAJgFxS4C+AUGzUWgE4BmcAMbQAykThgEACwD2wHFAgAPNEToJYiFGizJyBeElQYANFGQViZKmyi1GUBMB5cA5idtRgNdBCZEAV3QAIwgeDztBGQAbaIhBbxkiADk4XyYnFyJ3TxodBCZsAG0AXQ92NiiiJ0cIOISZHiYAMWi4YABFALCQTDEJaTlMQ20MUn0qKCjY+MSUtL8oAHJEOj4lqHYAei2oAD09th2oZJlFHh5GqEBQcg4+AKIEmiSvCCdMY3RscGhlVXVNEYdB86N9IKQzMhQT9LCRSAAKACUNjsoEg5h0AGkICANAQACJyU7AdrPIjY3F9cSSWTyT5giCQ6Hg0ieY4HNm7ABE9C5UAAPlAud5fHzBVzpvU5qlRQKhXkMAgxVAAAYK9AIAB0ABJKJk3CwVZ5PGjoABhGJSskyiAMiitdqqTAAcQg8n6NKG9J+TIZEOWktm1oWS3GnP2hzsx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Mks1QElDInmR0qYXQXw9lAJ2YZ2BAA
π» Code
π Actual behavior
Typescript isn't able to assign
{ collectionName: 'asdf' }
when using generic types:Without generic types it work seamlessly:
π Expected behavior
{ collectionName: 'asdf' }
should be successfully assignable toFlatQuery<Snapshot<ItemType, IdType>>
using generic types.Additional information about the issue
Related issue: maxnowack/signaldb#1030
The text was updated successfully, but these errors were encountered: