Search code examples
typescripttsc

Typescript: readonly property in class cannot be accessed


export declare class Config {
    readonly ROUTE_TYPE_CREATE: string;
}

import { Config } from "./Config";

export default class NewComponent {
    constructor() {
        this.routeType = Config.ROUTE_TYPE_CREATE;
    }
}

When I compile code, it returns:

ERROR in [at-loader] ./new.cpn.ts:12:33 
    TS2339: Property 'ROUTE_TYPE_CREATE' does not exist on type 'typeof Config'.

What is the problem? Why is it unaccessible?


Solution

  • You declared an instance property on Config, but not a static property named Config.ROUTE_TYPE_CREATE. Add static and it should work:

    declare class Config {
        static readonly ROUTE_TYPE_CREATE: string; 
    }
    

    The error message hints at this, if you read it very carefully:

    Property 'ROUTE_TYPE_CREATE' does not exist on type 'typeof Config'.

    If you had an instance, that would read on type 'Config', but you're working with the class directly.