A
arktype•2mo ago
Bobakanoosh

Passing an exported scope to a function (Type 'string' is not assignable to type 'never')

Hello, I have an exported scope that I'm trying to pass to a generic class to be used.
// the class
class SomeClass<TData extends any> {
// I tried `Type<TData>` here but get this when trying to call `this.schema()` "Type 'unknown' has no call signatures"
constuctor(public schema: Type) {
}
}

// the schema
const UserConfigSchema = scope({
config: {
windowMeta: "Record<string, windowMeta>",
},
windowMeta: {
position: {
x: "number",
y: "number",
},
}
}).export();

type UserConfig = typeof UserConfigSchema.config.infer;

class SomeParentClass extends SomeClass<UserConfig> {
constructor() {
// ERROR:
// Type 'string' is not assignable to type 'never'
super(UserConfigSchema.config);
}
}
// the class
class SomeClass<TData extends any> {
// I tried `Type<TData>` here but get this when trying to call `this.schema()` "Type 'unknown' has no call signatures"
constuctor(public schema: Type) {
}
}

// the schema
const UserConfigSchema = scope({
config: {
windowMeta: "Record<string, windowMeta>",
},
windowMeta: {
position: {
x: "number",
y: "number",
},
}
}).export();

type UserConfig = typeof UserConfigSchema.config.infer;

class SomeParentClass extends SomeClass<UserConfig> {
constructor() {
// ERROR:
// Type 'string' is not assignable to type 'never'
super(UserConfigSchema.config);
}
}
I see that .export() yields type Module, but I couldn't figure out how to use that either. Appreciate any help
7 Replies
ssalbdivad
ssalbdivad•2mo ago
Try using type.Any<TData> for schema. Some change recently seems to have made generics with vanilla Type less cooperative as a base constraint: https://discord.com/channels/957797212103016458/1302682752818352249/1306075236755570728 I'll see if I can easily address that but if not type.Any is a more accurate base type anyways since it allows you to pass a type from a scope
Bobakanoosh
BobakanooshOP•2mo ago
wow awesome that was easy, thanks so much, spent a crazy amount of time trying to figure it out on my own lol edited the title so hopefully anyone else with that error can find
ssalbdivad
ssalbdivad•2mo ago
Yeah it's really weird honestly, basically under the hood Type is a conditional that gets additional methods/props based on what kind of type it is. But all branches evaluate to BaseType plus those extra methods, and that would work in most cases as a base type in the past Hmm, I guess it might have just been the NoInfer stuff I added to make .map work better. @Andarist Did you have any luck with that NoInfer repro? Unfortunately I kind of need that to work around another case where TS behaves very weirdly 😛 (I mean I need the NoInfer logic to stay somehow, not that you have to fix anything in TS for me haha)
Bobakanoosh
BobakanooshOP•2mo ago
Ahh okay all good that kinda makes sense 😂 I was refactoring some code while doing this and realizing I actually have the wrong type, gonna make a new thread for this other issue..
Andarist
Andarist•2mo ago
Not yet, i have it on my todo list (aka somewhere in my tabs)
ssalbdivad
ssalbdivad•2mo ago
Oh so I nerdsniped you back into this haha Would love to find a workaround here now that it's affected a few people externally I'll mess around a bit more based on what you said.
Andarist
Andarist•2mo ago
not sure, haha - i got to the NoInfer issue from the other thread i thought i was replying to it, actually

Did you find this page helpful?