profile
viewpoint

Ask questionsErrors in typescript 4.1

Typescript 4.1 changes some things and ts-results does not work.

https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/

I am not entirely sure, but it can be seen here

https://www.typescriptlang.org/play?ts=4.1.0-dev.20201015#code/JYWwDg9gTgLgBAbwPIGsA0cCiUoYEoCmAzgK4A2MAvnAGZQQhwDkMRAtFMea0wNwBQ-GiQB2AYxjAIIuJwCGAEwBiwMgQAUYOTAAWALjhEYUYCIDmASgOFSFADxGT5jE1MA3OWWAK4W3UwA+RH44ULhgGjhNbR0LYLCE2QIYEigZEQIAdzhUaN0LXjgAeiK4DOzgIjgIMElpTzgdAk4QsOoCMiICeMTQzhS0sqysHHUAIndPb18YsYLi0vLwqpq6kQamloTKfh3+ErgAFQBPMGIxE1qyiGyaaDFiOGOIEjgYCDgxJrEUOEym3TNJ4vRpyNzdORwDxeHycWzwbSfBhgVTdSQgAgAOn4YmkRiS8LgAF4kooVGp1CxiDBMTAAB4wJgFfgRKJw7iYiAoOIIVqhA4wAgMpEiGByUxVQG0VFMKq40UEUV8kX4wXCknsiiY6ECBLyogQNSYsgQMzqNUwCy7OAdLo9fmlZpQG0ARxIniqrhE0Omfh0TGV+vgTuJBI5OsDeMNWJNZqdVsoQA

Type 'Err<string>' is not assignable to type 'Result<string, "invalid path">'. Type 'Err<string>' is not assignable to type 'Err<"invalid path"> & BaseResult<string, "invalid path">'. Type 'Err<string>' is not assignable to type 'Err<"invalid path">'. Type 'string' is not assignable to type '"invalid path"'.
import {Ok, Err, Result} from 'ts-results';

function readFile(path: string): Result<string, 'invalid path'> {
    if (path) {
        return new Ok(path); // new is optional here
    } else {
        return new Err("invalid path"); // new is optional here
    }
}

// Typescript now forces you to check whether you have a valid result at compile time.
const result = readFile('test.txt');
if (result.ok) {
    // text contains the file's content
    const text = result.val;
    console.log(text)
} else {
    // err equals 'invalid path'
    const err = result.val;
    console.log(err)
}
vultix/ts-results

Answer questions kevinsimper

@vultix Thanks for responding, so the example in the README.md has to be updated when typescript 4.1 is released? https://github.com/vultix/ts-results#example

Because the example does not work in 4.1 as is.

useful!

Related questions

No questions were found.
source:https://uonfu.com/
answerer
Kevin Simper kevinsimper Blackbeard Copenhagen, Denmark https://www.kevinsimper.dk Working on electric car sharing at @greenmobility
Github User Rank List