0

I'm new to TS so the question might be trivial to some of You, however I'm kind of missing something,

I'm trying to create a function that will return an array containing the users geolocation data like lingitude, latitude. I want this to be correctly covered by the TS type checking magic.

export const getUsersGeolocationData = (): [number,number] => {
    const navigatorResult: [number, number] = navigator.geolocation.getCurrentPosition(position =>{
        return [position.coords.latitude, position.coords.longitude];
    })
    return navigatorResult
}

console.log(getUsersGeolocationData())

My understanding: I'm letting TS know that I expect the [number, number] array as the result of getUsersGeolocationData() but I keep receiving the Type 'void' is not assignable to type '[number, number]'

Can somebody help me with understanding and fixing the issue ? :) Best regards !

5
  • 2
    What is a signature of getCurrentPosition? Commented Dec 1, 2020 at 21:55
  • What do You mean by "signature" ? Commented Dec 1, 2020 at 21:56
  • A function's "signature" is the types on its parameters and return type. The return type is the important one for your error: getCurrentPosition doesn't return anything. Commented Dec 1, 2020 at 21:56
  • Oh, it gives undefined / void . So how Can i get it working ? Commented Dec 1, 2020 at 21:59
  • 1
    You should promisify this call in order to get a result Commented Dec 1, 2020 at 22:01

3 Answers 3

1

If this is the API I am thinking it is, it looks like getCurrentPosition accepts a callback as the first argument that doesn't returnanything.

If you want this to work, perhaps you should consider turning it into a promise and consuming it that way.

export const getUsersGeolocationData = (): Promise<[number,number]> => {
    return new Promise((resolve, reject) => {
        navigator.geolocation.getCurrentPosition(position =>{
            resolve([position.coords.latitude, position.coords.longitude])
        })
    })
}

getUsersGeolocationData().then(res => console.log(res))
Sign up to request clarification or add additional context in comments.

1 Comment

So it's the matter of the async nature of the getUsersGeolocationData that i couldn't get it working and it had nothing to do with TS .... Thank You so much for the explanation :)
1

You should turn a call of this function into async function in order to get a result:

export const getUsersGeolocationData = (): Promise<[number,number]> => {
    return new Promise(resolve, reject) => {
      navigator.geolocation.getCurrentPosition(position =>{
        resolve([position.coords.latitude, position.coords.longitude]);
      })
    });
}

...
getUsersGeolocationData().then(coords => console.log(coords))

Comments

1
const getCurrentPosition = navigator.geolocation.getCurrentPosition.bind(navigator.geolocation);
const getCurrentPositionAsync = () => new Promise(getCurrentPosition);
export const getUsersGeolocationData = async () => {
    const position = await getCurrentPositionAsync();
    return [position.coords.latitude, position.coords.longitude] as const;
}

// then you call it:
getUsersGeolocationData().then(result => console.log(result));

// or from inside async function:
async function main() {
    console.log(await getUsersGeolocationData());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.