Skip to content

CookieStorage.getItem() type definition should include undefined #14663

@mikurikuri11

Description

@mikurikuri11

Before opening, please confirm:

  • I have searched for duplicate or closed issues
  • I have read the guide for submitting bug reports
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue

Category

Authentication (amazon-cognito-identity-js)

Bug Description

The CookieStorage.getItem() method's type definition returns string, but the actual implementation returns string | undefined.

Current type definition (index.d.ts):

export class CookieStorage implements ICognitoStorage {
    getItem(key: string): string;  // ← Missing undefined
}

Actual implementation (CookieStorage.js):

getItem(key) {
    return Cookies.get(key);  // js-cookie returns undefined when key doesn't exist
}

The js-cookie library's `Cookies.get(key)` returns `undefined` when the key doesn't exist, not an empty string.

Expected Behavior

The type definition should match the actual runtime behavior:

export class CookieStorage implements ICognitoStorage {
    getItem(key: string): string | undefined;
}

Reproduction

import { CookieStorage } from 'amazon-cognito-identity-js';

const storage = new CookieStorage({ domain: 'example.com' });
const value = storage.getItem('non-existent-key');

// TypeScript thinks: value is string
// Runtime reality: value is undefined
console.log(typeof value);  // "undefined"
console.log(value === undefined);  // true

Code Snippet

// This code has a bug that TypeScript cannot catch:
const value = storage.getItem(key);
if (value !== '') {  // Developer expects empty string for missing key
    return value;    // undefined passes this check!
}

Package version

amazon-cognito-identity-js@6.x

Additional context

Note: The `ICognitoStorage` interface correctly defines `getItem(key: string): string | null`, but `CookieStorage` class overrides this with just `string`, which is both incorrect and misleading.

Metadata

Metadata

Assignees

No one assigned

    Labels

    AuthRelated to Auth components/categoryV5amazon-cognito-identity-jsUsed for issues related to this specific package within the monorepoquestionGeneral question

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions