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

Excessive stack depth comparing types 'SchemaTypeDef<?>' and 'SchemaTypeDef<?>' #8535

Closed
tamaina opened this issue Apr 23, 2022 · 10 comments · Fixed by #8547
Closed

Excessive stack depth comparing types 'SchemaTypeDef<?>' and 'SchemaTypeDef<?>' #8535

tamaina opened this issue Apr 23, 2022 · 10 comments · Fixed by #8547
Labels
🔥high priority packages/backend Server side specific issue/PR 💚Refactor Rewriting code without changing behavior

Comments

@tamaina
Copy link
Contributor

tamaina commented Apr 23, 2022

#8332 由来のエラー。

admin/emoji/add-aliases-bulkのような単純な型でも発生する。

https://github.com/misskey-dev/misskey/blob/f23d5a75f4cb64328a99e25f8a17150fe5b8a4d3/packages/backend/src/server/api/endpoints/admin/emoji/add-aliases-bulk.ts

(Arrayが問題らしい?)

@tamaina tamaina added packages/backend Server side specific issue/PR 💚Refactor Rewriting code without changing behavior labels Apr 23, 2022
@tamaina
Copy link
Contributor Author

tamaina commented Apr 24, 2022

問題なのはArrayではなくて、関数定義まわりが原因なんだと思われる

image

https://www.typescriptlang.org/play?#code/PTAEEEHEAUBlQMwDoAMpAdJKAzgTwK4CWANgPYA0oALgIYC21BAdtaNYwCagmUAWApgCdQAWQJYsAaz45QAYxKNKAggCM8lEgKwAobXwAeAB02U5CrKYF8AZllABeUAG9tASGElZEgEo2AXM7aoMFUOIZ8AQDkJCoAVnyylJGsdvKMFmRBoAC+KWbplJnZANy6BsYCppRhfKDQ1F587AA8+qAGlHwcdlI4JNah4f2gVrYAfA6gAMqy-PQAKjXN1UMDo1gA2voAumOl2iu1i+FTSkwA5jaTkYx4RETJAD6gkSokJER8bE8vTJ2XAh+NzwtBUgiBFmUjHOQOoAgE1BwQJi8USsMYSNKh2mZ2hABEbPMSMc+M15u19J1uqASacoZdrBN7G5yR0uuw7MD7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cUoajDn6N3Ua3cBSDe7AwoZnkscdflL7oaY1Rj9OAsNjB1WaxjCkxPRD-CBve4HHygSg2+HtPUUUvseah0kgn7iTpE3JljCh-zsfigWBjIVRMscw+x5T3U1M8b5Ci+DD7HH9h04wN9BnO1HrVCZ0W9nv1SdNeA-FAgSb4YAzZoLe8-NGZc-vBYNRKXGco-O8LX9YxfLDUKymimHNSxUzusCyW+Nvn7YV8zp7rRFWJRUUlFi327yljxxwHadxrw3t2PBO8KCHvS4YeK+xGs4TSBYUAhg4R0EpZME8hwojpqSJkE+jbh0BBPK4EJW3aJREVRQS1FUogNN6H4EJyRshreu8VWd7AAg2fYKOi2eRJuUH2MlWQXErClS1khlG+hmjQBjigvdo0ZsIloAEEHR3-tjTySl6ggcQdjCSXzFJ+0AhBJCfsfQEPZu0EpRQCHWB0cnisJQPAAgUYbErM2dg2wLZgKAA

@syuilo
Copy link
Member

syuilo commented Apr 24, 2022

なるほど

@syuilo
Copy link
Member

syuilo commented Apr 26, 2022

これはダメだけど
image

これは良いらしい(<T>を消した)
image

@syuilo
Copy link
Member

syuilo commented Apr 26, 2022

これはダメだけど
image

ここまで単純化した型でもエラーになるからどうしようもなさそう

@syuilo
Copy link
Member

syuilo commented Apr 26, 2022

あ、objectのrequired対応部分を消したらエラー出なくなったから、原因はそこっぽい
required関係の型定義を工夫することで回避できる可能性あるかも

https://www.typescriptlang.org/play?#code/PTAEEEHEAUBlQMwDoAMpAdJKAzgTwK4CWANgPYA0oALgIYC21BAdtaNYwCagmUAWApgCdQAWQJYsAaz45QAYxKNKAggCM8lEgKwAobXwAeAB02U5CrKYF8AZllABeUAG9tASGElZEgEo2AXM7aoMFUOIZ8AQDkJCoAVnyylJGsdvKMFmRBoAC+KWbplJnZANy6BsYCppRhfKDQ1F587AA8+qAGlHwcdlI4JNah4f2gVrYAfA6gAMqy-PQAKjXN1UMDo1gA2voAumOl2iu1i+FTSkwA5jaTkYx4RETJAD6gkSokJER8bE8vTJ2XAh+NzwtBUgi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IH2Zi2TY4LFO4Gbe2NmzUGra+APsMPFfYUGoZpAsKAQwcI6ARMmCeQ4URPlJEyHzJF2lIpVkoq4E+AQ0ZUftNVSIFAZkVSiIg3ofgT7JGyHRsTxVLIEGsgEHN7BopFDyIRyg+xkqyC4lYUqBtrV430M0aACMf1g1GiRhEtAAgGd49psa-9piYf01gMYT9QBHxPvtAIrn2D7H0CZ0jtAIkUBM1gZzJ4rCUDwAIPGGxKzNnYNsbQ2RTZAA

@acid-chicken
Copy link
Member

type NullOrUndefined<p extends Schema, T> =
	p['nullable'] extends true
		?	p['optional'] extends true
			? (T | null | undefined)
			: (T | null)
		: p['optional'] extends true
			? (T | undefined)
			: T;

この方がブランチ短い

type NullOrUndefined<p extends Schema, T> =
	| (p['nullable'] extends true ? null : never)
	| (p['optional'] extends true ? undefined : never)
	| T;

@tamaina
Copy link
Contributor Author

tamaina commented Apr 26, 2022

ObjTypeを交差型でないようにしたらいけた

export type ObjType<s extends Obj, RequiredProps extends keyof s> =
	{ -readonly [P in keyof s]: P extends RequiredPropertyNames<RequiredProps, s> ? SchemaType<s[P]> : SchemaType<s[P]> | undefined };

ただOptionalとhoge | undefinedは異なるので、どうするか

@tamaina
Copy link
Contributor Author

tamaina commented Apr 26, 2022

export type ObjType<s extends Obj, RequiredProps extends keyof s> =
	UnionToIntersection<{
		-readonly [P in keyof s]?: SchemaType<s[P]>;
	} | {
		-readonly [S in RequiredPropertyNames<RequiredProps, s>]: SchemaType<s[S]>;
	}>;

あーうん、UnionToIntersectionってTypeScriptの型検査機を騙すから、これでいけるんだよな

@tamaina
Copy link
Contributor Author

tamaina commented Apr 26, 2022

これでいいか

@tamaina
Copy link
Contributor Author

tamaina commented Apr 26, 2022

TypeScriptはinferで騙されるので、unionではなくintersectionを突っ込んでもおkっぽい

export type ObjType<s extends Obj, RequiredProps extends keyof s> =
	UnionToIntersection<{
		-readonly [P in keyof s]?: SchemaType<s[P]>;
	} & {
		-readonly [S in RequiredPropertyNames<RequiredProps, s>]: SchemaType<s[S]>;
	}>;

tamaina added a commit to tamaina/misskey that referenced this issue Apr 26, 2022
Co-authored-by: acid-chicken <root@acid-chicken.com>
syuilo pushed a commit that referenced this issue Apr 27, 2022
* Fix #8535 Excessive stack ... 'SchemaTypeDef<?>'

Co-authored-by: acid-chicken <root@acid-chicken.com>

* add comment

* clean up

Co-authored-by: acid-chicken <root@acid-chicken.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔥high priority packages/backend Server side specific issue/PR 💚Refactor Rewriting code without changing behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants