ts-pattern icon indicating copy to clipboard operation
ts-pattern copied to clipboard

Wrong type with Pattern#when

Open ninesunsabiu opened this issue 2 years ago • 6 comments

Describe the bug I try to use ts-pattern with fp-ts when I use P.when(some_type_guard_function) is has not expected type

Code Sandbox with a minimal reproduction case the ts version of codesandbox editor is not support 4.7 yet

here is some case, I don't know what happen

image image

but when I change the guard function signature to (fa: any) => fa is Some<number> or (fa: unknown) => fa is Some<number>, it will work!!

image image

It has same problem when input type is string | number

image image image

but Tuple or Object can work

image

and I known match().when() always correct.

Versions

  • TypeScript version: 4.7.2
  • ts-pattern version: 4.0.2
  • environment: chrome

ninesunsabiu avatar Jun 03 '22 16:06 ninesunsabiu

there is another case

image

It does not work.

but, look at the type Data, when I delete value property or make it to optional, work again !!

image image

ninesunsabiu avatar Jun 03 '22 17:06 ninesunsabiu

here is my tsconfig.json

{
    "compilerOptions": {
        /* Projects */
        "incremental": true,

        /* Language and Environment */
        "target": "es6",
        "lib": ["dom", "esnext"],
        "jsx": "react-jsx",

        /* Modules */
        "module": "commonjs",
        "baseUrl": "./src",
        "paths": {
            "@/*": ["./*"]
        },
        "rootDirs": ["./src", "./package.json"],
        "types": ["node", "jest"],
        "resolveJsonModule": true,

        /* Emit */
        "noEmit": true,
        "sourceMap": true,
        "importsNotUsedAsValues": "error",

        /* Interop Constraints */
        "allowSyntheticDefaultImports": true,
        "forceConsistentCasingInFileNames": true,

        /* Type Checking */
        "strictNullChecks": true,
        "strictBindCallApply": true,
        "strictFunctionTypes": true,
        "strictPropertyInitialization": true,
        "noImplicitAny": false,
        "noImplicitThis": true,
        "useUnknownInCatchVariables": true,
        "noUncheckedIndexedAccess": true,

        /* Completeness */
        "skipLibCheck": true
    },
    "exclude": ["node_modules", "src/libs"],
    "include": ["src"]
}

ninesunsabiu avatar Jun 03 '22 17:06 ninesunsabiu

Hey! Thanks for the report, do you mind creating a TypeScript playground example demonstrating the issue you are facing so I can investigate later? it seems like you can import ts-pattern and fp-ts in there: Playground Example

gvergnaud avatar Jun 07 '22 08:06 gvergnaud

Hey! Thanks for the report, do you mind creating a TypeScript playground example demonstrating the issue you are facing so I can investigate later? it seems like you can import ts-pattern and fp-ts in there: Playground Example

Wowww I didn't known I can import npm package in TypeScript Playground.

this is first comment show case: Comment One Case

second comment show case: Comment Two Case

ninesunsabiu avatar Jun 07 '22 09:06 ninesunsabiu

Thanks for the playgrounds. Sorry I didn't have the time to look into it yet, I'll try to find some time this weekend

gvergnaud avatar Jun 10 '22 09:06 gvergnaud

never mind. I tried another way to write my code

ninesunsabiu avatar Jun 11 '22 07:06 ninesunsabiu