Argument of type 'string | undefined' is not assignable to parameter of type '"untitled" | null | undefined'

argument of type is not assignable to parameter of type
argument of type 'reacttext'
argument of type is not assignable to parameter of type 'component'
argument of type 'this' is not assignable to parameter of type 'construct'.
argument of type 'object' is not assignable to parameter of type 'string'
is not assignable to parameter of type 'ngmodule types of property 'providers' are incompatible
argument of type '()' is not assignable to parameter of type react
argument of type '( static: boolean; )' is not assignable to parameter of type '( read?: any)

I can't understand something about TypeScript's type inference. I thought this should be valid and wouldn't require me to specify the type of T:

export const deflt = <T>(val: (T | undefined | null), defaultVal: T): T => {
  if (val === undefined || val === null) {
    return defaultVal;
  }
  return val;
}

function maybeLabelName(name: string | undefined) {
  return deflt(name, 'untitled');
}

The call to deflt fails with the following error:

Argument of type 'string | undefined' is not assignable to parameter of type '"untitled" | null | undefined'.
  Type 'string' is not assignable to type '"untitled" | null | undefined'. [2345]

I can fix it by changing the deflt call to one of the following:

// Option 1:
return deflt<string>(name, 'untitled');

// Option 2:
const s: string = 'untitled';
return deflt(name, s);

Shouldn't TypeScript infer the string type automatically?

I think this is a bit of a quirk in the way typescript infers T in this case. Because you specify the constant 'untitled', the compiler will infer T as the string literal type 'untitled', since that is the simplest site to infer T from. It will then check the other parameter against the string literal type and find that string is not assignable to 'untitled'. A work around would be to decrease the priority of the second parameter when inferring T by adding an extra intersection with {}:

export const deflt = <T>(val: (T | undefined | null), defaultVal: T & {} ): T => {
    if (val === undefined || val === null) {
        return defaultVal;
    }
    return val;
}

function maybeLabelName(name: string | undefined) {
    return deflt(name, 'untitled'); // T is string now
}

Another solution (if ou can't change the original function) would be to explicitly assert untitled to string, which is basically what you already tried.

function maybeLabelName(name: string | undefined) {
    return deflt(name, 'untitled' as string); // T is string now
}

TS2345 Argument of type A is not assignable to parameter of type A , declare function decorate<T>(): (component: T) => T; class Foo { derp () {} } decorate<Foo>()(Foo);. error TS2345: Argument of type 'typeof Foo' is  This is what "argument of type 'char*' is incompatible with parameter of type 'char**'" means. To fix this, simply pass in a char **; you can do this by passing in the address of newArr instead of newArr itself: tOf = isPalindrome(&newArr, numb); //calling of function

When you're using generics, there can be two different ways of "generifying" a parameter.

Using your case as an example:

const deflt = <T>(val: (T | undefined | null), defaultVal: T): T

If you use: deflt<string>(a, b), then you're forcing a to be string | undefined | null and b to be string.

If you use: deflt(a: string, b: "untitled"), the transpiler understand "untitled" as a constant, not as a string. Therefore, b will be of type "untitled", thus making T be of type "untitled". As you're using a of type string, the compiler can't cast string to "untitled".

The solution is already provided by @Titian Cernicova-Dragomir answer

Argument of type 'this' is not assignable to parameter of type , Type 'HitCounter' is not assignable to type 'Construct'. Types of property 'node' are incompatible. Type 'import("/Users/dev_user/code/aws/cdk/  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more C++ argument of type is incompatible with parameter of type

You've declared deflt as a generic function, but have used it as a regular function

const deflt = <T>(val: (T | undefined | null), defaultVal: T): T => {
    if (val === undefined || val === null) {
      return defaultVal;
    }
    return val;
}

function maybeLabelName(name?: string) {
    return deflt<typeof name>(name, 'untitled');
}

Generic Type, While type argument inference can be a helpful tool to keep code shorter and more readable, you may need to explicitly pass in the type arguments as we did in  1. Python Function Argument. Previously, we have covered Functions in Python.In this Python Function Arguments tutorial, we will learn about what function arguments used in Python and its type: Python Keyword Arguments, Default Arguments in Python, and Python Arbitrary Arguments.

Types of Argument, There are three basic structures or types of argument you are likely to encounter in Each of these serves a different purpose, and deciding which type to use  const crawlRequet: CrawlRequest Argument of type 'CrawlRequest' is not assignable to parameter of type 'DeepPartial<CrawlRequest>'. Types of property 'iterations' are incompatible. Type 'CrawlIteration[]' is not assignable to type 'DeepPartial<CrawlIteration>[]'. Type 'CrawlIteration' is not assignable to type 'DeepPartial<CrawlIteration>'.

EXP47-C. Do not call va_arg with an argument of the incorrect type, This macro accepts the va_list representing the variable arguments of the function invocation and the type denoting the expected argument type for the argument  So I think the warning should really say argument of type "const char*" is incompatible with parameter of type "char*". I think the compiler is confused because it's an intrinsic (i.e. there is no prototype in the header file).

The Argument of Mathematics, mathematics education lies in the importance of Type 4 evaluations in mathematical argumentation.3 Mathematical proof seems to set argument admissibility in  Conditional types in which the checked type is a naked type parameter are called distributive conditional types. Distributive conditional types are automatically distributed over union types during instantiation. For example, an instantiation of T extends U ? X : Y with the type argument A | B | C for T is resolved as (A extends U ? X : Y) | (B

Comments
  • What version of TypeScript do you use?
  • @finico You ca reproduce in the latest TS, you just need strictNullCkecks turned on
  • I'm using TypeScript 3.1.6.
  • This feels almost like a bug.
  • @Nurpax you could post it to GitHub .. but this also infers the string literal type export const deflt = <T>(val: T): T => { return val; } deflt('untitled'); // T is '"untitled"'
  • Admittedly this really strikes me as counterintuitive and rather odd behavior. Do you think this is worth an issue against Typescript or would it just be "works as designed"? Edit: too slow :)
  • I don't think TypeScript requires me to specify the type arguments to generic functions. Type inference should handle that. See e.g., typescriptlang.org/docs/handbook/generics.html "Notice that we didn’t have to explicitly pass the type in the angle brackets (<>); the compiler just looked at the value "myString", and set T to its type."
  • Yes, it's a bug https://github.com/Microsoft/TypeScript/issues/9366
  • Excellent, found a couple of similar bugs while filing a bug against TypeScript and decided not to file a new bug. Thanks for the link!