Skip to content

Allow @type { import ... } to define classes #24467

Closed
@SimonMeskens

Description

@SimonMeskens

TS version: 2.9+ (Relevant issues: #14377 (comment), #14844, )

test.d.ts

export interface TestProperties {
    bar: string;
    baz: number;
}

export class Test {
    foo(param: TestProperties);
}

test.js

/**
 * @type { import("./test").Test }
 */
export class Test {
    foo({ bar, baz }) {
        bar = 7;
    }
}

Expected behavior: Throw an error when trying to assign 7 to bar
Actual behavior: no error

If I make this change, it works:

test.js

/**
 * @type { import("./test").Test }
 */
export class Test {
    /**
     * @param { import("./test").TestProperties } param
     */
    foo({ bar, baz }) {
        bar = 7;
    }
}

Correct behavior: Type '7' is not assignable to type 'string'.

Giving Test the correct type definition should also give foo the correct definition. Ideally, we wouldn't even need to do either and we should be able to just do:

/** @typedef { import("./test") } Test */

Or just be able to have TS pick up on the fact that we defined a .d.ts file for our .js file

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions