How to fix Default type of constructor property must be assignable to its declared type in TypeScript

TypeScriptINTERMEDIATEMEDIUMUpdated Dec 8, 2025ยท 106 views

This TypeScript error occurs when a constructor parameter property has a default value whose type does not match the declared type of the property. It's a type safety check ensuring that default values are compatible with the property type. Aligning the default value type with the property declaration resolves this issue.

Quick fix

Fixes Default type of constructor property must be assignable to its declared type

// ERROR: Default type 'number' is not assignable to declared type 'string'
class User {
  constructor(
    public name: string = 123  // WRONG - default is number, not string
  ) {}
}
  1. Identify the mismatched property and its types
  2. Ensure default value matches the declared type
  3. Handle nullable types with union types
  4. Verify literal types match exactly
typescript โ€” error
// ERROR: Default type 'number' is not assignable to declared type 'string'
class User {
constructor(
public name: string = 123 // WRONG - default is number, not string
) {}
}
Default type of constructor property must be assignable to its declared type

What this error means

This error indicates a type mismatch between a constructor parameter property's declared type and its default value. When using TypeScript's parameter property shorthand (prefixing constructor parameters with public, private, protected, or readonly), the default value you assign must be compatible with the property's explicit type annotation. TypeScript's type checker ensures that if you declare a property as a specific type, its default value must be assignable to that type. This prevents runtime errors where a property might contain an unexpected value type. The error commonly occurs when declaring a property as one type (like string) but providing a default value of another incompatible type (like number), or when the default value could be null/undefined but the property type doesn't allow those values.

Typical symptoms

  • TypeScript compiler shows error about default type not being assignable
  • Constructor parameter properties are highlighted in red in your IDE
  • Build process fails when running `npm run build` or `tsc`
  • Error message points to a specific constructor parameter with a default value
  • The error appears even though JavaScript would accept the default value
  • Error occurs specifically with parameter properties (public/private/protected/readonly parameters)

Common causes

  1. Default value type does not match the declared property type (e.g., declaring string but defaulting to number)
  2. Property declared as non-nullable but default value is null or undefined
  3. Default value is a more general type than the declared specific type (e.g., default is "any" but property is "string")
  4. Using incorrect literal type in default value (e.g., property expects "red" | "blue" but default is "green")
  5. Default value is an object/array that doesn't match the declared interface or type structure
  6. Property type is narrower than the inferred type of the default value
  7. Mixing optional property syntax with default values incorrectly
  8. Type inference from default value conflicts with explicit type annotation

How to fix "Default type of constructor property must be assignable to its declared type"

1Identify the mismatched property and its types

Read the error message to locate which constructor parameter property has the type mismatch. The error will indicate the declared type and the actual default value type.

typescript
// ERROR: Default type 'number' is not assignable to declared type 'string'
class User {
  constructor(
    public name: string = 123  // WRONG - default is number, not string
  ) {}
}
2Ensure default value matches the declared type

Change the default value to match the declared property type. Make sure the literal value or expression you use for the default is assignable to the property type.

typescript
// CORRECT - default value matches declared type
class User {
  constructor(
    public name: string = "Guest"  // OK - both are string
  ) {}
}

// CORRECT - number default for number type
class Counter {
  constructor(
    public count: number = 0  // OK - both are number
  ) {}
}
3Handle nullable types with union types

If you want to allow null or undefined as a default value, explicitly include these in the type annotation using a union type.

typescript
// ERROR - null not assignable to string
class User {
  constructor(
    public email: string = null  // WRONG
  ) {}
}

// CORRECT - include null in type
class User {
  constructor(
    public email: string | null = null  // OK - null is now valid
  ) {}
}

// OR use undefined for optional
class User {
  constructor(
    public email?: string  // OK - implicitly string | undefined, no default needed
  ) {}
}
4Verify literal types match exactly

When using literal types (specific strings, numbers, or union of literals), ensure the default value is one of the allowed literal values.

typescript
type Status = "active" | "inactive" | "pending";

// ERROR - "unknown" is not in the Status union
class Account {
  constructor(
    public status: Status = "unknown"  // WRONG
  ) {}
}

// CORRECT - use a valid literal from the union
class Account {
  constructor(
    public status: Status = "pending"  // OK - "pending" is in Status
  ) {}
}
5Match object/array structures to declared interfaces

For complex types like objects or arrays, ensure the default value structure matches the declared type exactly, including all required properties.

typescript
interface Config {
  host: string;
  port: number;
}

// ERROR - missing required property 'port'
class Server {
  constructor(
    public config: Config = { host: "localhost" }  // WRONG
  ) {}
}

// CORRECT - include all required properties
class Server {
  constructor(
    public config: Config = { host: "localhost", port: 3000 }  // OK
  ) {}
}
6Let TypeScript infer the type if appropriate

If the explicit type annotation is causing issues and you want the property type to be inferred from the default value, remove the type annotation and let TypeScript infer it.

typescript
// With explicit type - must match
class Settings {
  constructor(
    public timeout: number = 5000  // type must be number
  ) {}
}

// Let TypeScript infer - type becomes number from default
class Settings {
  constructor(
    public timeout = 5000  // inferred as number
  ) {}
}
7Use type assertion for complex initialization

If you have a complex default value that you're certain matches the type but TypeScript cannot verify, use a type assertion with as. Use this sparingly.

typescript
interface ComplexConfig {
  [key: string]: string | number;
}

// If TypeScript has trouble inferring
class App {
  constructor(
    public config: ComplexConfig = { timeout: 1000 } as ComplexConfig
  ) {}
}
8Review strictNullChecks and strict mode settings

Check your TypeScript configuration. With strict mode or strictNullChecks enabled, null and undefined are not assignable to most types unless explicitly allowed.

json
{
  "compilerOptions": {
    "strict": true,           // Enables all strict checks
    "strictNullChecks": true  // null/undefined must be explicit in types
  }
}

With strict mode, adjust your types accordingly:

typescript
// With strict mode OFF - this might work
class User {
  constructor(public name: string = null) {}  // null might be allowed
}

// With strict mode ON - must be explicit
class User {
  constructor(public name: string | null = null) {}  // OK - null is allowed
}

Advanced notes

Constructor parameter properties are a TypeScript shorthand feature that combines parameter declaration and class property initialization in one step. When you use visibility modifiers (public, private, protected) or readonly on constructor parameters, TypeScript automatically creates a class property and assigns the parameter value to it. This feature is purely syntactic sugar that makes class definitions more concise. The type checking rules for parameter properties are the same as for regular class properties: the assigned value (including default values) must be assignable to the declared type. One subtle consideration is the interaction between optional parameters (using ?) and default values. A parameter like public name?: string is equivalent to public name: string | undefined, which means undefined is a valid value. However, providing an explicit default value (like public name: string = "default") makes the parameter optional at call time but ensures the property is never undefined at runtime. When working with complex types, particularly discriminated unions or conditional types, TypeScript may struggle to verify that a default value matches the declared type; in such cases, consider using factory methods or separate initialization logic instead of constructor defaults. The strictPropertyInitialization flag (part of strict mode) requires that all class properties be initialized either at declaration or in the constructor, which affects how you structure your parameter properties and defaults.

Frequently asked questions

What does "Default type of constructor property must be assignable to its declare" mean?
This error indicates a type mismatch between a constructor parameter property's declared type and its default value. When using TypeScript's parameter property shorthand (prefixing constructor parameters with public, private, protected, or readonly), the default value you assign must be compatible with the property's explicit type annotation. TypeScript's type checker ensures that if you declare a property as a specific type, its default value must be assignable to that type.
How do I fix "Default type of constructor property must be assignable to its declare"?
This TypeScript error occurs when a constructor parameter property has a default value whose type does not match the declared type of the property. It's a type safety check ensuring that default values are compatible with the property type. Aligning the default value type with the property declaration resolves this issue.
What causes "Default type of constructor property must be assignable to its declare"?
Default value type does not match the declared property type (e.g., declaring string but defaulting to number) Property declared as non-nullable but default value is null or undefined Default value is a more general type than the declared specific type (e.g., default is "any" but property is "string")
Is the "Default type of constructor property must be assignable to its declare" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading