TypeScript Errors: Type is not assignable to the expected type

Problem: Error TS2322: Type is not assignable to the expected type.

[{
	"owner": "typescript",
	"code": "2322",
	"severity": 8,
	"message": "Type '(_parent: {}, args: { id: string; }, ctx: ContextType) => Promise<{ _id: string; roles: { roleType: string | null; roleName: string | null; fullAccess_allFeatures: boolean; fullAccess_allFeatures_excluding_organization_ownership: boolean; privileges: string[]; userTasks: string[]; }[]; createdAt: string; updatedAt: ...' is not assignable to type 'FieldResolver<\"Query\", \"roleTemplate\">'.\n  Type 'Promise<{ _id: string; roles: { roleType: string | null; roleName: string | null; fullAccess_allFeatures: boolean; .\n Types of property 'roleName' are incompatible.\n Type 'string | null' is not assignable to type 'string'.\n                          Type 'null' is not assignable to type 'string'.",
	"source": "ts",
	"startLineNumber": 175,
	"startColumn": 13,
	"endLineNumber": 175,
	"endColumn": 20,
	"relatedInformation": [
		{
			"startLineNumber": 158,
			"startColumn": 5,
			"endLineNumber": 158,
			"endColumn": 12,
			"message": "The expected type comes from property 'resolve' which is declared here on type 'NexusOutputFieldConfig<\"Query\", \"roleTemplate\">'",
			"resource": "/home/user/Desktop/work/web-portal/Backend/api-main/node_modules/nexus/dist/definitions/definitionBlocks.d.ts"
		}
	]
}]

How It Occurred:

  1. Type Mismatch: The resolver function’s return type does not match the expected type defined in the schema. The fields roleType and roleName are defined as string | null in the resolver but are expected to be strictly string in the schema.
  2. Promise Type Handling Issue: The resolver is returning a Promise with a type that does not conform to the Nexus or other middleware’s expected Promise type.
  3. Schema Configuration Issue: The schema does not allow null values for fields, but the data source may return null for some fields.

Solution:

const roleTemplate = {
                        _id: result.id,
                        roles: roles.map((role: any) => ({
                            roleType: role.roleType || null, // added null for type safety
                            roleName: role.roleName || null,
                            fullAccess_allFeatures: role.fullAccess_allFeatures || false,
                            fullAccess_allFeatures_excluding_organization_ownership: role.fullAccess_allFeatures_excluding_organization_ownership || false,
                            privileges: role.privileges || [],
                            userTasks: role.userTasks || [],
                        })),
                        createdAt: result.createdAt || "",
                        updatedAt: result.updatedAt || "",
                    };
                    return roleTemplate

How to Resolve:

  1. Check the Schema/Type Definition: If the fields can legitimately be null, update the schema to allow them to be nullable (e.g., roleType: string | null).
  2. Update the Resolver Function: Provide default values for nullable fields in the resolver or adjust the return type to match the schema’s expectations.
  3. Ensure Type Compatibility: Make sure your resolver’s return type strictly matches the schema’s type definition. This helps prevent such type mismatches.

References:

5 Likes