Skip to content

doesn't work return params for function #46603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ilchenkoArtem opened this issue Oct 30, 2021 · 6 comments
Closed

doesn't work return params for function #46603

ilchenkoArtem opened this issue Oct 30, 2021 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@ilchenkoArtem
Copy link

Bug Report

🔎 Search Terms

doesn't work return params for function
function return parmas doesn't work
return function params with generic type don't work

🕗 Version & Regression Information

  • This is a crash
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about 'return type function type with generic'

⏯ Playground Link

Playground link with relevant code #1

Playground link with relevant code #2

💻 Code

#1

type Types = string | number;

type Obj1 = { x: 10 };
type Obj2 = { x: 20 };

type GetType<T extends Types> = T extends string 
    ? Obj1
    : T extends number 
        ? Obj2 
        : never


const fn = <T extends Types>(a: T):GetType<T> => {
    if (typeof a === 'string') {
        return {x: 10} //there should be no mistake. Should be has correct type by generic
    }

    if (typeof a === 'number') {
        return {x: 20} //there should be no mistake. Should be has correct type by generic
    }

    throw Error('test')
}

const x = fn(10)

#2

type Obj1 = { x:10 }
type Obj2 = { x: 20 }
type Obj3 = { x: 30 }

function fn(a: string): Obj1
function fn(a: number): Obj2
function fn(a: boolean): Obj3
function fn(a: unknown): Obj1 | Obj2 | Obj3 | never {
    if (typeof a === 'string') return {x: 10};
    if (typeof a === 'number') return {x: 20}
    if (typeof a === 'boolean') return {x: 20} //Must be erorr :(

    throw new Error("test")
}

const x = fn(true)

🙁 Actual behavior

#1 has error Type '{ x: 10; }' is not assignable to type 'GetType'.(2322);

#2 has not error with wrong return type

🙂 Expected behavior

#1 there should be no mistake. Should be has correct type by generic
#2 has error with wrong return type

@MartinJohns
Copy link
Contributor

This is a crash

This is not a crash.


Number 2 is working as intended. The implementation is not checked against the overloaded signatures. It's a duplicate of #13235.

@ilchenkoArtem
Copy link
Author

This is a crash

This is not a crash.

Number 2 is working as intended. The implementation is not checked against the overloaded signatures. It's a duplicate of #13235.

Thanks. What about the first option?

@jcalz
Copy link
Contributor

jcalz commented Oct 30, 2021

That’s a duplicate of #33912 I think

@4lessandrodev
Copy link

I also have a similar error

const data = ["a", "b", "c", 1, 2, 3];

// Type '(string | number)[]' is not assignable to type 'number[]'
const result: number[] = data.filter((i) => typeof i === 'number');

console.log(result); // [1, 2, 3] 

@MartinJohns
Copy link
Contributor

@4lessandrodev That's completed unrelated, and is working as intended. Your inner function is not a type guard, it's just a function returning a boolean. As a result it's not used to narrow the type. You can add an explicit type annotation to make it a type guard.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 1, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants