Skip to content

Issues with generic constraints #110

@sidney-pauly

Description

@sidney-pauly

The problem

There are some problems with the current way generic constraints are handled
Those issues include:

  • Having another generic type as a constraint (The generic type will also have a extends keyword)
  • Having a interface as a constraint (They are simply ignored)
  • Multiple constraints for one generic parameter (applies to interfaces)
  • Having circular constraints (Results in stack overflow)

Another potential issues are generic parameter attributes like new() or class. I'm not sure if and how they should be translated into typescript.

Minimal examples

c# classes

    [ExportTsClass]
    public class RecursiveConstraintClass<TSelf>
                where TSelf : RecursiveConstraintClass<TSelf>
    {
    }

    [ExportTsInterface]
    public interface IRecursiveConstraintInterface<TSelf>
        where TSelf : IRecursiveConstraintInterface<TSelf>
    {

    }

    [ExportTsInterface]
    public interface IRecursiveConstraintInterfaceWithClassConstraint<TSelf>
        where TSelf : class, IRecursiveConstraintInterfaceWithClassConstraint<TSelf>, new()
    {

    }

    [ExportTsInterface]
    public interface ICicrularConstraintInterface<TSelf, TOther>
        where TSelf : ICicrularConstraintInterface<TSelf, TOther>
        where TOther : ICicrularConstraintInterface<TOther, TSelf>
    {

    }


    public interface IA
    {

    }

    public interface IB
    {

    }

    [ExportTsInterface]
    public interface IMultipleConstraintInterface<T>
        where T : IA, IB
    {

    }

Typescript output

Expected (RecursiveConstraintClass)

/**
 * This is a TypeGen auto-generated file.
 * Any changes made to this file can be lost when this file is regenerated.
 */

export class RecursiveConstraintClass<TSelf extends RecursiveConstraintClass<TSelf>> {

}

Actual

Stackoverflow exception

Expected (IRecursiveConstraintInterface)

/**
 * This is a TypeGen auto-generated file.
 * Any changes made to this file can be lost when this file is regenerated.
 */

export interface IRecursiveConstraintInterface<TSelf extends IRecursiveConstraintInterface<TSelf>> {

}

Actual

StackoverflowException

Expected (IRecursiveConstraintInterfaceWithClassConstraint)

/**
 * This is a TypeGen auto-generated file.
 * Any changes made to this file can be lost when this file is regenerated.
 */

export interface IRecursiveConstraintInterfaceWithClassConstraint<TSelf extends IRecursiveConstraintInterfaceWithClassConstraint<TSelf>> {

}

Actual

StackoverflowException

Expected (ICicrularConstraintInterface)

/**
 * This is a TypeGen auto-generated file.
 * Any changes made to this file can be lost when this file is regenerated.
 */

export interface ICicrularConstraintInterface<TSelf extends ICicrularConstraintInterface<TSelf, TOther>, TOther extends ICicrularConstraintInterface<TOther, TSelf>> {

}

Actual

StackoverflowException

Expected (IMultipleConstraintInterface)

/**
 * This is a TypeGen auto-generated file.
 * Any changes made to this file can be lost when this file is regenerated.
 */

import { IA } from "./ia";
import { IB } from "./ib";

export interface IMultipleConstraintInterface<T extends IA & IB> {

}

Actual

/**
 * This is a TypeGen auto-generated file.
 * Any changes made to this file can be lost when this file is regenerated.
 */

export interface IMultipleConstraintInterface<T> {

}

Solution

I will shortly add a pull request addressing this and some other issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions