microsoft / microsoft/TypeScript
WebGL(2) context methods don't allow null for resource parameters
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
WebGL2RenderingContext, attachShader, createShader, null
🕗 Version & Regression Information
- This is the behavior in every version I tried (Nightly, 5.4.5, 4.9.5, 3.9.7), and I reviewed the FAQ for entries about WebGL
⏯ Playground Link
💻 Code
export type ShaderType = WebGL2RenderingContext['FRAGMENT_SHADER'] | WebGL2RenderingContext['VERTEX_SHADER'];
export function loadShader(gl: WebGL2RenderingContext, source: string, type: ShaderType): WebGLShader | null {
const shader = gl.createShader(type);
// if you uncomment those lines it works but goes against the recomendations at https://www.khronos.org/webgl/wiki/HandlingContextLost#Don%E2%80%99t_check_for_null_on_creation
// if (shader === null) {
// throw new Error("shader is null, you've lost context");
// }
gl.shaderSource(shader, source);
gl.compileShader(shader);
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success) {
return shader;
}
console.error(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
🙁 Actual behavior
Context methods that take in WebGL resources (shader, program, buffer...) report errors when provided with null but create* methods return the resource (pointer) or null, and the spec says we should not check for null upon resource creation because all functions accept null as a parameter (no-op).
example :
gl.shaderSource(shader, source);
Argument of type 'WebGLShader | null' is not assignable to parameter of type 'WebGLShader'.
Type 'null' is not assignable to type 'WebGLShader'.
🙂 Expected behavior
Context methods taking resources as parameters should allow for null to be passed in. This would delineate from MDN's documentation but fit closer to the actual behavior of the API and the WebGL(2) spec.
Additional information about the issue
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the WebGL2RenderingContext declarations used by the linked TypeScript playground and review resource-taking methods such as shaderSource, compileShader, and getShaderParameter. Compare their parameter types with the nullable results of createShader and the linked WebGL context-loss guidance. Done means the relevant WebGL resource parameters accept null and the playground no longer reports the shown diagnostic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- web-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100