Skip to content

Compiler API - Type parameter constraint and default always undefined #20137

Closed

Description

TypeScript Version: 2.7.0-dev.2011118

Code

Test code:

let t: <T extends string = string>() => void;

Compiler code:

const type = ...; // get type of VariableDeclaration
const signature = type.getCallSignatures()[0]; 
const typeParam = signature.getTypeParameters()[0];
typeParam.constraint; // undefined
typeParam.default; // undefined

Expected behavior: A string type will be returned for both.

Actual behavior: Always returns undefined.

typescript.d.ts says the following:

interface TypeParameter extends TypeVariable {
    /** Retrieve using getConstraintFromTypeParameter */
    constraint: Type;
    default?: Type;
}

Problems

  • Both properties are always undefined (from why I've tried).
  • getConstraintFromTypeParameter does not exist anywhere in the compiler api.
  • constraint: Type should have a question token.

Workaround

  1. Get type's symbol.
  2. Get symbol's type parameter declaration.
  3. Get type via type checker from type parameter declaration's constraint and default nodes (if they exist).

Activity

  1. added
    Bug
    Domain: API
    on Nov 20, 2017
  2. mhegazy commented on Nov 20, 2017

    @mhegazy
    Contributor

    We need to make these internal and instead expose getDefault and getConstraint on a TypeObject.

  3. changed the title [-]Compiler API - Signature constraint and default always undefined[/-] [+]Compiler API - Type parameter constraint and default always undefined[/+] on Nov 20, 2017
  4. added
    Fixed
    on Nov 28, 2017
  5. dsherret commented on Nov 29, 2017

    @dsherret
    ContributorAuthor

    Thanks guys!

  6. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugDomain: APIFixed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions