Skip to content

infer of selective types #39737

Closed
Closed
@Akimotorakiyu

Description

@Akimotorakiyu

Search Terms

infer of selective types

Suggestion

infer of selective types

Use Cases

interface FunInterface {
  < R,P>(params: P): R;
}

let fun: FunInterface ;

let p ="123";
fun<number>("123");
// should be equivalent with `fun<number, typeof p>(p)`
// should be equivalent with `fun<number, string>(p)`

one more thing, Generics by default

function name(p){
  return p
}
// should be  equivalent with
// function name<P>(p:P){
//  return p
// }

const aName = name("someName")
// type of aName is string
const aNumber = name(123)
// type of aNumber is number

Examples

function deal<A extends unknown[], E>(event: E){
  return {
    event,
    fun:(fn:(...args:A))=>{
      eventBus.on(event,fn);
    }
  }
}

const eventName = "notify" as SomeEventType;

deal<[number]>(eventName );
// should be equivalent with `fun<[number], typeof eventName>(eventName );`
// should be equivalent with `fun<[number], SomeEventType>(eventName );`

one more thing, Generics by default

function name<A extends unknown[], B extends SomeType>(p1, a:A, p2:AKindType,  b: B){
  return {p1,a,p2,b}
}
// should be  equivalent with
// function name<A extends unknown[], B extends SomeType,T>(p1:T, a:A, p2:AKindType,  b: B){
//  return {p1,a,p2,b}
// }

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions