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

AVLayer, ShapeLayer, and TextLayer Type Boundaries Are Not Clearly Defined #139

Open
loneprison opened this issue Dec 7, 2024 · 2 comments

Comments

@loneprison
Copy link

I encountered an issue with the type definitions for AVLayer, ShapeLayer, and TextLayer in the Types-for-Adobe . The current implementation defines both ShapeLayer and TextLayer as extends AVLayer. This causes problems when narrowing down layer types with type guards like isAVLayer, isShapeLayer, and isTextLayer.

For instance, when you try to narrow down the layer type to be either AVLayer, ShapeLayer, or TextLayer, TypeScript treats both ShapeLayer and TextLayer as subtypes of AVLayer due to inheritance. This causes the following issue: after narrowing with isAVLayer, TypeScript sees the else branch as a never type, preventing access to methods that should be available on ShapeLayer or TextLayer.

This leads to a situation where methods specific to ShapeLayer or TextLayer are inaccessible unless you use type assertions, which defeats the purpose of having separate type guards like isShapeLayer and isTextLayer.

Moreover, it is evident that AVLayer , ShapeLayer ,TextLayer are not overlapping

Here's a code example demonstrating the issue:

function isAVLayer(layer: Layer): layer is AVLayer {
    return layer instanceof AVLayer;
}

function isShapeLayer(layer: Layer): layer is ShapeLayer {
    return layer instanceof ShapeLayer;
}

function isTextLayer(layer: Layer): layer is TextLayer {
    return layer instanceof TextLayer;
}

var firstLayer = (app.project.activeItem as CompItem).selectedLayers[0];

if (isAVLayer(firstLayer) || isShapeLayer(firstLayer) || isTextLayer(firstLayer)) {
    if (isAVLayer(firstLayer)) {
        firstLayer.effect.addProperty("ADBE Gaussian Blur 2");
        alert("is AVLayer");
    } else if (isTextLayer(firstLayer)) {
        firstLayer.effect.addProperty("ADBE Gaussian Blur 2");
        alert("is TextLayer");
    } else if (isShapeLayer(firstLayer)) {
        firstLayer.effect.addProperty("ADBE Gaussian Blur 2");
        alert("is ShapeLayer");
    }
}

In this example, TypeScript incorrectly types the else branch as never, making it impossible to access methods on ShapeLayer or TextLayer unless manually type-asserted.

@zlovatt
Copy link
Member

zlovatt commented Dec 7, 2024

Happy to review a PR for this!

@loneprison
Copy link
Author

Thank you for your response! I'm still quite new to TypeScript and not entirely sure how to resolve this issue. I hope someone else can help address this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants