Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setting, deleting and retrieving cookies in Typescript.
@aacassandra
Copy link

aacassandra commented May 4, 2019

thanks brother, its works

@Piratenlulasch
Copy link

Piratenlulasch commented Jun 12, 2019

Got an Error "Parts might be undefined" on line 20, didn't quite know how to fix that properly so i added a // @ts-ignore.
Works fine now.

@joduplessis
Copy link
Author

joduplessis commented Jun 12, 2019

Hi @Piratenlulasch, mind sharing bit of the context of how you're using it? Might be worth adding a check for parts before getting its length property. Maybe something like:
if (!parts) return null or if (!parts) throw new Error('Null cookie')

@Piratenlulasch
Copy link

Piratenlulasch commented Jun 12, 2019

Hey @joduplessis, I tried a few things along those lines and your suggestions don't work ether. I think the .pop can return undefined. Maybe that's the problem?
Anyway, this is my code right now:

export function getCookie(name: string) {
    const value = "; " + document.cookie;
    const parts = value.split("; " + name + "=");

    if (parts.length == 2) {
        // @ts-ignore
        return parts.pop().split(";").shift();
    }
}

There is nothing else in my file, except for your other two methods.

Edit: Fixed code formatting and added syntax highlighting

@Andromelus
Copy link

Andromelus commented Mar 2, 2020

@michael-freidgeim-webjet

I tried https://gist.github.com/floriancastelain/4a6063edd927c59351b43fa1b3f57b94 version, but have the same error, that original gist has
[tsl] ERROR in c:common\cookieUtils.ts(20,16)
TS2532: Object is possibly 'undefined'.
Adding if (!parts) return null doesn't help.
Only // @ts-ignore before return parts.pop(). helps

@wellingtonfds
Copy link

wellingtonfds commented Sep 11, 2020

thanks guy!!

@ricocheting
Copy link

ricocheting commented Oct 8, 2020

The error Object is possibly 'undefined' appears to be from typescript thinking .pop() can return an undefined result (even though the length check should prevent that).

For me it goes away with:

if (parts.length === 2) {
	const ppop = parts.pop();
	if (ppop) {
		return ppop.split(";").shift();
	}
}

@tp00012x
Copy link

tp00012x commented Oct 30, 2020

You are missing the return type for getCookie.

@benediktvaldez
Copy link

benediktvaldez commented Mar 17, 2021

Just commenting to say that with the latest typescript you could solve the pop undefined scenario like this

export function getCookie(name: string) {
    const value = "; " + document.cookie;
    const parts = value.split("; " + name + "=");
    
    if (parts.length == 2) {
        return parts.pop()?.split(";").shift();
    }
}

Alternatively if you'd like to consistently return a string you could do it like this

  return (
    (parts.length === 2 &&
      parts
        .pop()
        ?.split(";")
        .shift()) ||
    ""
  );

@kittanat-mos
Copy link

kittanat-mos commented Jun 11, 2021

thank you krub

@AbbottF
Copy link

AbbottF commented May 14, 2022

Thank you. This has helped me weening myself from jQuery Libraries...and morphing my js to ts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment