Skip to content
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

lib.d.ts Function.bind with this #28571

Closed
5 tasks
bluelovers opened this issue Nov 16, 2018 · 5 comments
Closed
5 tasks

lib.d.ts Function.bind with this #28571

bluelovers opened this issue Nov 16, 2018 · 5 comments
Labels
Unactionable There isn't something we can do with this issue

Comments

@bluelovers
Copy link
Contributor

Search Terms

Suggestion

i don't know what is better name for ITSOverwriteThisFunction

interface Function2 extends Function
{
	bind<T extends any, F extends (...args: any[]) => any>(this: F, thisArg: T, ...argArray: any[]): ITSOverwriteThisFunction<T, F>;
}

export type ITSOverwriteThisFunction<T extends any, F extends (...args: any[]) => any> =
	(this: T, ...args: Parameters<F>) => ReturnType<F>

Use Cases

  1. we can now let fn.bind, know who is this

problem: current typescript not support overload function, so this has limit

Examples

this examples looks so stupid, but i don't know how to make look better

export interface t4 extends Function2
{
	(): string
}

export declare let t5: t4

export let t6 = t5.bind([] as string[])

t6 = function ()
{
	this.includes('') // => this is string[]

	return ''
}

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.
@DanielRosenwasser
Copy link
Member

I'm a little bit confused over what's being suggested here, but is it covered by #27028 which is coming in TypeScript 3.2?

@bluelovers
Copy link
Contributor Author

bluelovers commented Nov 16, 2018

@DanielRosenwasser

i think it not same,

in t6, ts know it is string[], so will show error when use this.padEnd(1, '0')

but in t9 , ts didn't show error

export interface t4 extends Function2
{
	(): string
}

export declare let t5: t4

export let t6 = t5.bind([] as string[])

t6 = function ()
{
	this.includes('') // => this is string[]

	// ts will and should show error
	this.padEnd(1, '0')
	
	return ''
}

export interface t7 extends Function
{
	(): string
}

export declare let t8: t7

export let t9 = t8.bind([] as string[])

t9 = function ()
{
	this.includes('') // => this is string[]

	// ts didn't show error
	this.padEnd(1, '0')

	return ''
}

@DanielRosenwasser
Copy link
Member

I think that's because you extended Function, so all the "wrong" methods for bind are present. If you remove that, TS3.2 will resolve bind from a new type called CallableFunction. Can you verify whether that works?

@bluelovers
Copy link
Contributor Author

bluelovers commented Nov 16, 2018

my version is 3.2.0-dev.20181114

export interface t10
{
	(): string
}

export declare let t11: t10

export let t12 = t11.bind([] as string[])

t12 = function ()
{
	this.includes('') // => this is string[]

	// ts didn't show error
	this.padEnd(1, '0')

	return ''
}

export declare function t13()
export let t14 = t13.bind([] as string[])

t14 = function ()
{
	this.includes('') // => this is string[]

	// ts didn't show error
	this.padEnd(1, '0')

	return ''
}

image

@weswigham weswigham added the Needs Investigation This issue needs a team member to investigate its status. label Nov 19, 2018
@RyanCavanaugh RyanCavanaugh added Unactionable There isn't something we can do with this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Aug 23, 2019
@RyanCavanaugh
Copy link
Member

I can't tell what's going on here. The posted code is full of errors and I don't see which ones are supposed to be good or bad. Please open a new issue with a clear description if needed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unactionable There isn't something we can do with this issue
Projects
None yet
Development

No branches or pull requests

4 participants