Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array in conditional type issue #57901

Closed
marc0n3 opened this issue Mar 22, 2024 · 4 comments
Closed

array in conditional type issue #57901

marc0n3 opened this issue Mar 22, 2024 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@marc0n3
Copy link

marc0n3 commented Mar 22, 2024

πŸ”Ž Search Terms

array in conditional type issue inheritance accessor function

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYGwhgzhAECSAuBTAtgbwFDS9AdmZiAhAFwTwBOAljgOboC+6YARmeWMPNKJDAigGEA9jnhhqichmzRKSZBBJx5AbQC6DdOh5RoAEXYB3SdEQAPJDgAmfecNHick6dgiUAXkWI4ArsmaSmlpa8ACeAA6I0ACC5OyhsDhO5AAqEYgAPDEAfNAAvFjRphaI1jAAFNQAZibVkuSIVgCU6gD8dXGNxNEA3MHa4LoAsmB4NJIZKcWWNsqCImIS5NkuWMijYOPlQswAVoicxClNqzLQYZHQVDQAFvBplwWx8YnJD5n8yPaLySoARHIUBA-mpsn0ztgAPSQiEAPVamAh53S0EM5BENHe+WeYASSUk70m-0BChBYMREOh2HhFOgjHoQA

πŸ’» Code

class Item{
    name!:string
}
abstract class ItemContainer{
    items!: Item[]
}

class Drawer extends ItemContainer{
    size!:number
}




type ArrayInnerType< A> =  A extends (infer inferred)[]?inferred:A;



class Manager<T extends ItemContainer>{
    manage(object:T){
        type rightType = ArrayInnerType<ItemContainer["items"]>;
        //      ^?
        type wrongType =ArrayInnerType<T["items"]>;
        //   ^?
    }
}

πŸ™ Actual behavior

actual type of wrongType is (inferred & inferred[]) | T["items"]

πŸ™‚ Expected behavior

wrongType is Item

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

This is a design limitation. Resolving of conditional types involving unbound generic types is deferred. The compiler does not know what type T is, so it can't know what the conditional type resolves to.

@marc0n3
Copy link
Author

marc0n3 commented Mar 22, 2024

Thanks!
cannot the fact that a base class for T (ItemContainer) is defined help in getting at least base class property types?

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Mar 22, 2024
@RyanCavanaugh
Copy link
Member

It'd be wrong to produce Item here since T might be instantiated with a class with a more-specific type in that field, e.g.

class Item {
    name!: string
}
class SuperItem extends Item {
    isSuper!: true;
}
abstract class ItemContainer {
    items!: Item[]
}
class SuperItemContainer extends ItemContainer {
    items!: SuperItem[];
}

class Drawer extends ItemContainer {
    size!: number
}

type ArrayInnerType<A> = A extends (infer inferred)[] ? inferred : A;

class Manager<T extends ItemContainer> {
    manage(object: T): ArrayInnerType<T["items"]> {
        // Alleged: Should be just 'Item'
        type wrongType = ArrayInnerType<T["items"]>;
        // Then this is OK
        const p: wrongType = new Item();
        return p;
    }
}

const p = new Manager<SuperItemContainer>();
// x: SuperItem, but it's actually an Item
const x = p.manage(new SuperItemContainer());

@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Not a Defect This behavior is one of several equally-correct options labels Mar 22, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants